Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Commit

Permalink
<dev> adapt windows
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeNoobKing committed Jan 26, 2024
1 parent 7e5c6f2 commit cdd8c9d
Show file tree
Hide file tree
Showing 11 changed files with 143 additions and 15 deletions.
4 changes: 2 additions & 2 deletions arkctl/common/fileutil/file_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package fileutil
import (
"context"
"fmt"
"serverless.alipay.com/sofa-serverless/arkctl/common/osutil"
"strings"
)

Expand All @@ -25,8 +26,7 @@ type FileUrl string

func (url FileUrl) GetFileUrlType() FileUrlType {
switch {
// start with file:// then it's a local file
case strings.HasPrefix(string(url), "file://"):
case strings.HasPrefix(string(url), osutil.GetLocalFileProtocol()):
return FileUrlTypeLocal

default:
Expand Down
32 changes: 32 additions & 0 deletions arkctl/common/osutil/os_util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package osutil

import (
"runtime"
"strings"
)

func IsWindows() bool {
return strings.HasPrefix(strings.ToLower(runtime.GOOS), "win")
}

func GetLocalFileProtocol() string {
if IsWindows() {
return "file:///"
} else {
return "file://"
}
}
7 changes: 4 additions & 3 deletions arkctl/v1/cmd/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"fmt"
"os"
"path/filepath"
"serverless.alipay.com/sofa-serverless/arkctl/common/osutil"
"strings"

"serverless.alipay.com/sofa-serverless/arkctl/common/cmdutil"
Expand Down Expand Up @@ -146,7 +147,7 @@ func execMavenBuild(ctx *contextutil.Context) bool {

func execParseBizModel(ctx *contextutil.Context) bool {
style.InfoPrefix("Stage").Println("ParseBizModel")
bundlePath := "file://" + defaultArg
bundlePath := osutil.GetLocalFileProtocol() + defaultArg
if doBuild {
searchdir := defaultArg
if subBundlePath != "" {
Expand All @@ -164,7 +165,7 @@ func execParseBizModel(ctx *contextutil.Context) bool {
pterm.Error.Println("can not find pre built biz bundle in build dir!")
return false
}
bundlePath = "file://" + bundlePath
bundlePath = osutil.GetLocalFileProtocol() + bundlePath
}

bizModel, err := ark.ParseBizModel(ctx, fileutil.FileUrl(bundlePath))
Expand Down Expand Up @@ -289,7 +290,7 @@ func execInstallInKubePod(ctx *contextutil.Context) bool {
string(runtime.Must(json.Marshal(ark.BizModel{
BizName: bizModel.BizName,
BizVersion: bizModel.BizVersion,
BizUrl: fileutil.FileUrl("file://" + ctx.Value(ctxKeyArkBizBundlePathInSidePod).(string)),
BizUrl: fileutil.FileUrl(osutil.GetLocalFileProtocol() + ctx.Value(ctxKeyArkBizBundlePathInSidePod).(string)),
}))),
fmt.Sprintf("http://127.0.0.1:%v/installBiz", portFlag),
)
Expand Down
4 changes: 2 additions & 2 deletions arkctl/v1/service/ark/biz_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"archive/zip"
"context"
"fmt"
"serverless.alipay.com/sofa-serverless/arkctl/common/osutil"
"strings"

"serverless.alipay.com/sofa-serverless/arkctl/common/fileutil"
Expand All @@ -37,8 +38,7 @@ func parseJarBizModel(ctx context.Context, bizUrl fileutil.FileUrl) (*BizModel,
return nil, err
}

// remove file:// prefix
zipReader, err := zip.OpenReader(localPath[7:])
zipReader, err := zip.OpenReader(localPath[len(osutil.GetLocalFileProtocol()):])
if err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion arkctl/v1/service/ark/biz_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"io"
"os"
"path/filepath"
"serverless.alipay.com/sofa-serverless/arkctl/common/osutil"
"strings"
"testing"

Expand Down Expand Up @@ -74,5 +75,5 @@ func TestParseBizModel_LocalJar(t *testing.T) {

assert.Equal(t, model.BizName, "testName")
assert.Equal(t, model.BizVersion, "version")
assert.Equal(t, model.BizUrl, fileutil.FileUrl("file://"+zipFilePath))
assert.Equal(t, model.BizUrl, fileutil.FileUrl(osutil.GetLocalFileProtocol()+zipFilePath))
}
4 changes: 3 additions & 1 deletion arkctl/v1/service/ark/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"encoding/json"
"net"
"net/http"
"serverless.alipay.com/sofa-serverless/arkctl/common/fileutil"
"serverless.alipay.com/sofa-serverless/arkctl/common/osutil"
"testing"

"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -123,7 +125,7 @@ func TestInstallBiz_NoServer(t *testing.T) {
BizModel: BizModel{
BizName: "biz",
BizVersion: "0.0.1-SNAPSHOT",
BizUrl: "file:///foobar",
BizUrl: fileutil.FileUrl(osutil.GetLocalFileProtocol() + "/foobar"),
},
TargetContainer: ArkContainerRuntimeInfo{
RunType: ArkContainerRunTypeLocal,
Expand Down
5 changes: 5 additions & 0 deletions sofa-serverless-runtime/arklet-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
<groupId>com.alipay.sofa</groupId>
<artifactId>sofa-ark-api</artifactId>
</dependency>
<dependency>
<groupId>com.alipay.sofa.serverless</groupId>
<artifactId>sofa-serverless-common</artifactId>
<version>${revision}</version>
</dependency>

<dependency>
<groupId>com.github.oshi</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.alipay.sofa.serverless.arklet.core.common.log.ArkletLoggerFactory;
import com.alipay.sofa.serverless.arklet.core.common.model.BatchInstallRequest;
import com.alipay.sofa.serverless.arklet.core.common.model.BatchInstallResponse;
import com.alipay.sofa.serverless.common.util.OSUtils;
import com.google.inject.Singleton;

/**
Expand Down Expand Up @@ -67,7 +68,7 @@ public ClientResponse safeBatchInstall(String bizUrl) {
BizOperation bizOperation = new BizOperation()
.setOperationType(BizOperation.OperationType.INSTALL);

bizOperation.putParameter(Constants.CONFIG_BIZ_URL, "file://" + bizUrl);
bizOperation.putParameter(Constants.CONFIG_BIZ_URL, OSUtils.getLocalFileProtocolPrefix() + bizUrl);
Map<String, Object> mainAttributes = batchInstallHelper.getMainAttributes(bizUrl);
bizOperation.setBizName((String) mainAttributes.get(Constants.ARK_BIZ_NAME));
bizOperation.setBizVersion((String) mainAttributes.get(Constants.ARK_BIZ_VERSION));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class UnifiedOperationServiceImplTests {
private UnifiedOperationServiceImpl unifiedOperationService;

@Spy
private BatchInstallHelper batchInstallHelper;
private BatchInstallHelper batchInstallHelper;

@Before
public void setUp() {
Expand Down Expand Up @@ -131,14 +131,13 @@ public void testBatchInstall() {
}

BatchInstallResponse response = unifiedOperationService.batchInstall(BatchInstallRequest
.builder().bizDirAbsolutePath("/path/to/biz").build());
.builder().bizDirAbsolutePath("/path/to/biz").build());

Assert.assertTrue(response.getBizUrlToResponse().
containsKey("/file/a-biz.jar"));

containsKey("/file/a-biz.jar"));
Assert.assertTrue(response.getBizUrlToResponse().

containsKey("/file/b-biz.jar"));
containsKey("/file/b-biz.jar"));

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.alipay.sofa.serverless.common.util;

/**
* @author CodeNoobKing
* @data 2024/1/22
*/
public class OSUtils {

static String OS_NAME_KEY = "os.name"; // 操作系统名称。

/**
* 获取本地文件协议前缀。
*
* @return 本地文件协议前缀。
*/
public static String getLocalFileProtocolPrefix() {
String os = System.getProperty(OS_NAME_KEY);
if (os.toLowerCase().startsWith("win")) {
return "file:///";
} else {
return "file://";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.alipay.sofa.serverless.common.util;

import org.junit.Assert;
import org.junit.Test;

/**
* @author CodeNoobKing
* @data 2024/1/22
*/
public class OSUtilsTest {

@Test
public void testGetLocalFileProtocolPrefix() {
try {
OSUtils.OS_NAME_KEY = "mock.os.name";
System.setProperty("mock.os.name", "Windows 7");
Assert.assertEquals("file:///", OSUtils.getLocalFileProtocolPrefix());

System.setProperty("mock.os.name", "Linux");
Assert.assertEquals("file://", OSUtils.getLocalFileProtocolPrefix());

System.setProperty("mock.os.name", "Mac OS X");
Assert.assertEquals("file://", OSUtils.getLocalFileProtocolPrefix());
} finally {
OSUtils.OS_NAME_KEY = "os.name";
}

}
}

0 comments on commit cdd8c9d

Please sign in to comment.