Skip to content

Commit

Permalink
Merge pull request #2 from hamster-shared/feature/v1
Browse files Browse the repository at this point in the history
Feature/v1
  • Loading branch information
mohaijiang authored Apr 19, 2022
2 parents 44a6bb3 + 6102936 commit 704f6bf
Show file tree
Hide file tree
Showing 12 changed files with 102 additions and 61 deletions.
4 changes: 3 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/frontend/node_modules
#/frontend/dist
/frontend/package-lock.json
Dockerfile
hamster-gateway
#hamster-gateway
.dockerignore
36 changes: 9 additions & 27 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,47 +1,29 @@
FROM golang:1.17.3 as builder

# install cgo-related dependencies
RUN set -eux; \
apt-get update; \
apt-get install -y --no-install-recommends \
libvirt-dev \
gcc \
; \
rm -rf /var/lib/apt/lists/*

WORKDIR /usr/local/go/src/github.com/hamster-shared/hamster-gateway/

ENV GO111MODULE on
ENV GOPROXY https://goproxy.cn

COPY . .

RUN set -eux; \
go mod tidy ; \
go build

FROM node:17 as builder-2

FROM docker:20
WORKDIR /usr/local/go/src/github.com/hamster-shared/hamster-gateway/frontend

RUN set -eux; \
apk update ; \
apk add libvirt-dev
COPY . .

WORKDIR /root/
RUN npm install ;\
npm run build

## test evn
ENV CHAIN_ADDRESS 183.66.65.207:49944
ENV CPU 1
ENV MEMORY 1

COPY --from=builder /usr/local/go/src/tntlinking.com/ttchain-compute-provider/ttchain-compute-provider /usr/local/bin/
FROM ubuntu:20.04

RUN set -eux ;\
ttchain-compute-gateway init
COPY --from=builder /usr/local/go/src/github.com/hamster-shared/hamster-gateway/hamster-gateway /usr/local/bin/

COPY --from=builder-2 /usr/local/go/src/github.com/hamster-shared/hamster-gateway/frontend/dist /usr/local/bin/frontend/

CMD sed -i 's/"none"/"done"/' ~/.ttchain-compute-provider/config \
&& sed -i "s/127.0.0.1:9944/$CHAIN_ADDRESS/" ~/.ttchain-compute-provider/config \
&& sed -i "s/cpu\"\:1/cpu\"\:$CPU/" ~/.ttchain-compute-provider/config\
&& sed -i "s/mem\"\:1/mem\"\:$MEMORY/" ~/.ttchain-compute-provider/config\
&& ttchain-compute-provider daemon
CMD hamster-gateway daemon
22 changes: 5 additions & 17 deletions cmd/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,13 @@ package cmd

import (
"fmt"
gsrpc "github.com/centrifuge/go-substrate-rpc-client/v4"
"github.com/hamster-shared/hamster-gateway/core"
context2 "github.com/hamster-shared/hamster-gateway/core/context"
chain2 "github.com/hamster-shared/hamster-gateway/core/modules/chain"
"github.com/hamster-shared/hamster-gateway/core/modules/config"
"github.com/hamster-shared/hamster-gateway/core/modules/time"
"github.com/hamster-shared/hamster-gateway/core/modules/utils"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"os"
)

// daemonCmd represents the daemon command
Expand Down Expand Up @@ -77,23 +74,14 @@ func NewContext(accountId, chainAddress string) context2.CoreContext {
cfg.ChainApi = chainAddress
}
_ = cm.Save(cfg)
substrateApi, err := gsrpc.NewSubstrateAPI(cfg.ChainApi)
if err != nil {
logrus.Error(err)
os.Exit(1)
}
reportClient, err := chain2.NewChainClient(cm, substrateApi)
if err != nil {
logrus.Error(err)
return context2.CoreContext{}
}

timeService := utils.NewTimerService()
stateService := time.NewStateService(reportClient, cm)
stateService := time.NewStateService(cm)

context := context2.CoreContext{
Cm: cm,
ReportClient: reportClient,
SubstrateApi: substrateApi,
Cm: cm,
//ReportClient: reportClient,
//SubstrateApi: substrateApi,
TimerService: timeService,
StateService: stateService,
}
Expand Down
8 changes: 6 additions & 2 deletions core/corehttp/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ func setBootState(gin *MyContext) {
}

if op.Option {
gin.CoreContext.StateService.Start()
err := gin.CoreContext.StateService.Start()
if err != nil {
gin.JSON(http.StatusBadRequest, BadRequest(err.Error()))
}
} else {
gin.CoreContext.StateService.Stop()
}
Expand All @@ -64,7 +67,8 @@ func getBootState(gin *MyContext) {

func getP2pBW(gin *MyContext) {
nd := gin.CoreContext.StateService.Node
if !nd.IsOnline {

if nd == nil || !nd.IsOnline {
gin.JSON(http.StatusBadRequest, BadRequest("Not Online"))
return
}
Expand Down
31 changes: 20 additions & 11 deletions core/modules/time/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package time

import (
"fmt"
gsrpc "github.com/centrifuge/go-substrate-rpc-client/v4"
chain2 "github.com/hamster-shared/hamster-gateway/core/modules/chain"
"github.com/hamster-shared/hamster-gateway/core/modules/config"
"github.com/hamster-shared/hamster-gateway/core/modules/p2p"
Expand All @@ -20,19 +21,32 @@ type StateService struct {
cm *config.ConfigManager
}

func NewStateService(reportClient chain2.ReportClient, cm *config.ConfigManager) *StateService {
func NewStateService(cm *config.ConfigManager) *StateService {
return &StateService{
reportClient: reportClient,
cm: cm,
cm: cm,
}
}

func (s *StateService) Start() {
func (s *StateService) Start() error {

if s.Running() {
return
return nil
}
cf, err := s.cm.GetConfig()

substrateApi, err := gsrpc.NewSubstrateAPI(cf.ChainApi)
if err != nil {
log.Error(err)
return err
}
reportClient, err := chain2.NewChainClient(s.cm, substrateApi)
if err != nil {
log.Error(err)
return err
}

s.reportClient = reportClient

s.ctx, s.cancel = context.WithCancel(context.Background())

node, err := p2p.RunDaemon(s.ctx)
Expand All @@ -42,12 +56,6 @@ func (s *StateService) Start() {
os.Exit(1)
}

cf, err := s.cm.GetConfig()
if err != nil {
log.Error("run ipfs daemon fail")
os.Exit(1)
}

localAddress := fmt.Sprintf("/ip4/%s/tcp/%d/p2p/%s", cf.PublicIp, cf.PublicPort, node.Identity.String())

// 2: blockchain registration
Expand Down Expand Up @@ -77,6 +85,7 @@ func (s *StateService) Start() {
}
}(s.ctx)

return nil
}

func (s *StateService) Stop() {
Expand Down
2 changes: 2 additions & 0 deletions doc/history/ipfs/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FROM ipfs/go-ipfs
ENV IPFS_SWARM_KEY '/key/swarm/psk/1.0.0/\n/base16/\n55158d9b6b7e5a8e41aa8b34dd057ff1880e38348613d27ae194ad7c5b9670d7'
29 changes: 29 additions & 0 deletions doc/history/ipfs/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
version: '3.9'
services:
ipfs:
image: 'ipfs/go-ipfs:latest'
ports:
- "37774:37774"
- "5001:5001"
- "4001:4001"
restart: always
volumes:
- "ipfs_data:/data/ipfs"
- "./swarm.key:/data/ipfs/swarm.key"
entrypoint: /sbin/tini --
environment:
IPFS_PROFILE: badgerds
command: >-
/bin/sh -c "/usr/local/bin/start_ipfs config --json API.HTTPHeaders.Access-Control-Allow-Origin '[\"http://127.0.0.1:5001\",\"http://localhost:5001\", \"http://ipfs:5001\"]'
&& /usr/local/bin/start_ipfs config --json API.HTTPHeaders.Access-Control-Allow-Methods '[\"PUT\", \"POST\"]'
&& /usr/local/bin/start_ipfs config Addresses.Gateway /ip4/127.0.0.1/tcp/37774
&& /usr/local/bin/start_ipfs config Addresses.API /ip4/127.0.0.1/tcp/5001
&& /usr/local/bin/start_ipfs config Datastore.StorageMax 20GB
&& /usr/local/bin/start_ipfs config --json Swarm.RelayClient.Enabled true
&& /usr/local/bin/start_ipfs config --json Swarm.RelayService.Enabled true
&& /usr/local/bin/start_ipfs bootstrap rm --all
&& /usr/local/bin/start_ipfs bootstrap add /ip4/59.80.40.149/tcp/4001/p2p/12D3KooWSm8rYXfjbMaBkySrt1WhtHEZpqJXyqj36hPJoVkMvQfd
&& /usr/local/bin/start_ipfs daemon --migrate=true"
container_name: ipfs
volumes:
ipfs_data:
3 changes: 3 additions & 0 deletions doc/history/ipfs/swarm.key
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/key/swarm/psk/1.0.0/
/base16/
55158d9b6b7e5a8e41aa8b34dd057ff1880e38348613d27ae194ad7c5b9670d7
1 change: 1 addition & 0 deletions frontend/src/locales/lang/en/boot/boot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ export default {
accessPort: 'access port',
type: 'virtualization type',
resourceInfo: 'Resource information',
setStateFail: 'Set state fail',
};
1 change: 1 addition & 0 deletions frontend/src/locales/lang/zh-CN/boot/boot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ export default {
accessPort: '访问端口',
type: '虚拟化类型',
resourceInfo: '资源信息',
setStateFail: '修改状态失败',
};
10 changes: 9 additions & 1 deletion frontend/src/views/gateway/boot/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import { Switch } from 'ant-design-vue';
import { getBootStateApi, setBootStateApi } from '/@/api/gateway/boot';
import { getConfigApi } from '/@/api/gateway/initialization';
import { message } from 'ant-design-vue';
const { pkg } = __APP_INFO__;
const { name } = pkg;
Expand All @@ -42,7 +43,14 @@
const onChange = function (checked) {
loading.value = true;
setBootStateApi(checked)
.then(() => {})
.then(() => {
console.log("success")
})
.catch(err => {
console.log(err)
option.value = !checked
message.error(t('boot.boot.setStateFail'));
})
.finally(() => {
loading.value = false;
});
Expand Down
16 changes: 14 additions & 2 deletions frontend/src/views/gateway/bw/VisitAnalysis.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import { onMounted,onUnmounted, ref, Ref } from 'vue';
import { useECharts } from '/@/hooks/web/useECharts';
import {getBandwidthApi} from '/@/api/gateway/bw'
import { getBootStateApi } from '/@/api/gateway/boot';
defineProps({
...basicProps,
Expand All @@ -24,7 +25,18 @@
const timer = ref()
const getData = function () {
const ensureData = function () {
getBootStateApi().then(state => {
if(state){
getData()
}
})
}
const getData = function (){
getBandwidthApi().then(data => {
if(timeArray.value.length >= xAxisLength){
Expand Down Expand Up @@ -115,7 +127,7 @@
onMounted(() => {
timer.value = setInterval(getData,2000);
timer.value = setInterval(ensureData,2000);
});
Expand Down

0 comments on commit 704f6bf

Please sign in to comment.