Compare commits

..

11 Commits

Author SHA1 Message Date
Minghe
48413abaa1 add cov upload (#345) 2019-11-11 18:50:53 +08:00
Minghe
d36b2b935b fix lint 2019-11-11 18:15:55 +08:00
Minghe
f493749689 Better fx run remote docker host (#338) 2019-11-11 15:35:52 +08:00
dependabot-preview[bot]
9de10bc885 Bump github.com/spf13/viper from 1.4.0 to 1.5.0 (#339)
Bumps [github.com/spf13/viper](https://github.com/spf13/viper) from 1.4.0 to 1.5.0.
- [Release notes](https://github.com/spf13/viper/releases)
- [Commits](https://github.com/spf13/viper/compare/v1.4.0...v1.5.0)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
2019-11-02 08:45:15 +08:00
Minghe
2d5446686a fix docker workflow (#337) 2019-10-27 21:55:17 +08:00
Minghe
0d7d4f4a6a fix docker workflow (#336) 2019-10-19 08:02:42 +08:00
Minghe
23c4171ece update to up and down since we already merge them up (#335) 2019-10-18 09:56:22 +08:00
Minghe
d8a1868fce Merge "deploy" command to "up" command (#333) 2019-10-18 09:32:57 +08:00
Minghe Huang
d91a9959a8 bump version 2019-10-17 10:17:32 +08:00
Minghe
87e7c7d6ae fix the wrong binding when deploy function on Docker environment (#330) 2019-10-17 09:45:26 +08:00
Minghe
89c94daebc update contributors info (#329) 2019-10-16 23:57:42 +08:00
58 changed files with 1297 additions and 1990 deletions

View File

@@ -1,73 +0,0 @@
defaults: &defaults
machine: true
environment:
IMPORT_PATH: "github.com/$CIRCLE_PROJECT_USERNAME/$CIRCLE_PROJECT_REPONAME"
OUTPUT_DIR: "./build"
DIST_DIR: "./dist"
install_golang: &install_golang
run:
name: install Golang 1.11
command: |
sudo add-apt-repository ppa:gophers/archive
sudo apt-get update
sudo apt-get install golang-1.11-go
alias go="/usr/lib/go-1.11/bin/go"
go version
install_deps: &install_deps
run:
name: Install deps
command: |
/usr/lib/go-1.11/bin/go mod vendor
/usr/lib/go-1.11/bin/go get -u github.com/gobuffalo/packr/packr
install_httpie: &install_httpie
run:
name: install httpie
command: |
sudo apt-get -y update && sudo apt-get -y install httpie
install_jq: &install_jq
run:
name: install jq
command: |
sudo apt-get update && sudo apt-get -y install jq
build_binary: &build_binary
run:
name: build binary
command: |
/usr/lib/go-1.11/bin/go build -o ${OUTPUT_DIR}/fx fx.go
unit_test: &unit_test
run:
name: unit test
command: |
make unit-test
bash <(curl -s https://codecov.io/bash) -t ${CODECOV_TOKEN}
cli_test: &cli_test
run:
name: cli test
command: make cli-test
version: 2
jobs:
test:
<<: *defaults
steps:
- checkout
- *install_golang
- *install_deps
- *unit_test
- *build_binary
- run:
name: Pull images
command: make pull
- *cli_test
workflows:
version: 2
workflow:
jobs:
- test

View File

@@ -17,6 +17,11 @@ jobs:
run: | run: |
./scripts/provision.sh ./scripts/provision.sh
- name: lint
run: |
docker run --rm -v $(pwd):/app -w /app golangci/golangci-lint \
golangci-lint run -v
- name: setup k8s and kind - name: setup k8s and kind
run: | run: |
export GOBIN=$(go env GOPATH)/bin export GOBIN=$(go env GOPATH)/bin
@@ -33,7 +38,13 @@ jobs:
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }} DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
run: | run: |
export KUBECONFIG=/home/runner/.kube/kind-config-fx-test export KUBECONFIG=/home/runner/.kube/kind-config-fx-test
DEBUG=true go test -v ./container_runtimes/... ./deploy/... DEBUG=true go test -v ./...
- name: code cov
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
run: |
go test -race -coverprofile=coverage.txt -covermode=atomic ./...
bash <(curl -s https://codecov.io/bash) -t ${CODECOV_TOKEN}
- name: build fx - name: build fx
run: | run: |
@@ -47,15 +58,11 @@ jobs:
make test make test
# make docker-publish #TODO in release workflow # make docker-publish #TODO in release workflow
- name: lint
run: |
export GOBIN=$(go env GOPATH)/bin
export PATH=$PATH:$GOBIN
go get -u github.com/golangci/golangci-lint/cmd/golangci-lint
golangci-lint run
- name: test fx cli - name: test fx cli
env:
REMOTE_HOST_ADDR: ${{secrets.DOCKER_REMOTE_HOST_ADDR}}
REMOTE_HOST_USER: ${{secrets.DOCKER_REMOTE_HOST_USER}}
REMOTE_HOST_PASSWORD: ${{secrets.DOCKER_REMOTE_HOST_PASSWORD}}
run: | run: |
echo $KUBECONFIG echo $KUBECONFIG
unset KUBECONFIG unset KUBECONFIG
@@ -70,8 +77,8 @@ jobs:
if [[ -z "$AKS_KUBECONFIG" ]];then if [[ -z "$AKS_KUBECONFIG" ]];then
echo "skip deploy test since no valid KUBECONFIG" echo "skip deploy test since no valid KUBECONFIG"
else else
DEBUG=true ./build/fx deploy -n hello -p 12345 examples/functions/JavaScript/func.js DEBUG=true ./build/fx up -n hello -p 12345 examples/functions/JavaScript/func.js
./build/fx destroy hello ./build/fx down hello
rm ${KUBECONFIG} rm ${KUBECONFIG}
fi fi

View File

@@ -1,57 +1,70 @@
on: on: [push]
schedule: # schedule:
- cron: '0 12 * * *' # - cron: '0 12 * * *'
name: docker name: docker
jobs: jobs:
Docker: Docker:
runs-on: ubuntu-latest runs-on: ubuntu-latest
strategy:
matrix:
docker_version:
- 18.09
# - 19.03
# - 19.09
docker_channel:
- stable
# - test
steps: steps:
- uses: actions/checkout@master - uses: actions/checkout@master
- name: setup docker
uses: docker-practice/actions-setup-docker@master
with:
docker_version: ${{ matrix.docker_version }}
docker_channel: ${{ matrix.docker_channel }}
- name: login - name: login docker hub
uses: actions/docker/login@8cdf801b322af5f369e00d85e9cf3a7122f49108
env: env:
DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }} DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }} DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
run: |
docker login --username $DOCKER_USERNAME --password $DOCKER_PASSWORD
- name: build-fx-go-image - name: build and publish fx d image
uses: actions/docker/cli@master if: always()
with: run: |
args: build -t metrue/fx-go-base:latest -f api/asserts/dockerfiles/base/go/Dockerfile docker build -t metrue/fx-d-base:latest -f ./assets/dockerfiles/base/d/Dockerfile ./assets/dockerfiles/base/d
api/asserts/dockerfiles/base/go docker push metrue/fx-d-base:latest
- name: push-fx-go-image # - name: build and publish fx java image
uses: actions/docker/cli@master # run: |
# docker build -t metrue/fx-go-base:latest -f ./assets/dockerfiles/base/java/Dockerfile ./assets/dockerfiles/base/java
# docker push metrue/fx-java-base:latest
- name: build and publish fx node image
if: always()
run: |
docker build -t metrue/fx-node-base:latest -f ./assets/dockerfiles/base/node/Dockerfile ./assets/dockerfiles/base/node
docker push metrue/fx-node-base:latest
- name: build and publish fx python image
if: always()
run: |
docker build -t metrue/fx-python-base:latest -f ./assets/dockerfiles/base/python/Dockerfile ./assets/dockerfiles/base/python
- name: publish fx python image
env: env:
DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }} DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }} DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
with: run: |
args: push metrue/fx-go-base:latest docker push metrue/fx-python-base:latest
- name: build-fx-rust-image # - name: build and publish fx rust image
uses: actions/docker/cli@master # if: always()
with: # run: |
args: build -t metrue/fx-rust-base:latest -f api/asserts/dockerfiles/base/rust/Dockerfile # docker build -t metrue/fx-rust-base:latest -f ./assets/dockerfiles/base/rust/Dockerfile ./assets/dockerfiles/base/python
api/asserts/dockerfiles/base/rust # docker push metrue/fx-rust-base:latest
- name: push-fx-rust-image - name: build and publish fx julia image
uses: actions/docker/cli@master if: always()
env: run: |
DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }} docker build -t metrue/fx-julia-base:latest -f ./assets/dockerfiles/base/julia/Dockerfile ./assets/dockerfiles/base/julia
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }} docker push metrue/fx-julia-base:latest
with:
args: push metrue/fx-rust-base:latest
- name: build-fx-node-image
uses: actions/docker/cli@master
with:
args: build -t metrue/fx-node-base:latest -f api/asserts/dockerfiles/base/node/Dockerfile
api/asserts/dockerfiles/base/node
- name: push-fx-node-image
uses: actions/docker/cli@master
env:
DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
with:
args: push metrue/fx-node-base:latest

View File

@@ -22,6 +22,11 @@ jobs:
run: | run: |
./scripts/provision.sh ./scripts/provision.sh
- name: lint
run: |
docker run --rm -v $(pwd):/app -w /app golangci/golangci-lint \
golangci-lint run -v
- name: setup k8s and kind - name: setup k8s and kind
run: | run: |
export GOBIN=$(go env GOPATH)/bin export GOBIN=$(go env GOPATH)/bin
@@ -44,14 +49,6 @@ jobs:
run: | run: |
make build make build
- name: lint
run: |
export GOBIN=$(go env GOPATH)/bin
export PATH=$PATH:$GOBIN
go get -u github.com/golangci/golangci-lint/cmd/golangci-lint
golangci-lint run
- name: test fx cli - name: test fx cli
run: | run: |
echo $KUBECONFIG echo $KUBECONFIG
@@ -66,8 +63,8 @@ jobs:
run: | run: |
export KUBECONFIG=${HOME}/.kube/aks export KUBECONFIG=${HOME}/.kube/aks
echo ${AKS_KUBECONFIG} | base64 -d > $KUBECONFIG echo ${AKS_KUBECONFIG} | base64 -d > $KUBECONFIG
DEBUG=true ./build/fx deploy -n hello -p 12345 examples/functions/JavaScript/func.js DEBUG=true ./build/fx up -n hello -p 12345 examples/functions/JavaScript/func.js
./build/fx destroy hello ./build/fx down hello
rm ${KUBECONFIG} rm ${KUBECONFIG}
Release: Release:
runs-on: ${{ matrix.os }} runs-on: ${{ matrix.os }}

View File

@@ -7,6 +7,9 @@ lint:
generate: generate:
packr packr
b:
go build -o ${OUTPUT_DIR}/fx fx.go
build: build:
go build -o ${OUTPUT_DIR}/fx fx.go go build -o ${OUTPUT_DIR}/fx fx.go
@@ -24,7 +27,11 @@ unit-test:
./scripts/coverage.sh ./scripts/coverage.sh
cli-test: cli-test:
echo 'run testing on localhost'
./scripts/test_cli.sh ./scripts/test_cli.sh
# TODO enable remote test
echo 'run testing on remote host'
DOCKER_REMOTE_HOST_ADDR=${REMOTE_HOST_ADDR} DOCKER_REMOTE_HOST_USER=${REMOTE_HOST_USER} DOCKER_REMOTE_HOST_PASSWORD=${REMOTE_HOST_PASSWORD} ./scripts/test_cli.sh
http-test: http-test:
./scripts/http_test.sh ./scripts/http_test.sh

View File

@@ -4,6 +4,7 @@ Poor man's function as a service.
<br/> <br/>
![ci](https://github.com/metrue/fx/workflows/ci/badge.svg) ![ci](https://github.com/metrue/fx/workflows/ci/badge.svg)
![build](https://circleci.com/gh/metrue/fx.svg?style=svg&circle-token=bd62abac47802f8504faa4cf8db43e4f117e7cd7) ![build](https://circleci.com/gh/metrue/fx.svg?style=svg&circle-token=bd62abac47802f8504faa4cf8db43e4f117e7cd7)
[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fmetrue%2Ffx.svg?type=shield)](https://app.fossa.com/projects/git%2Bgithub.com%2Fmetrue%2Ffx?ref=badge_shield)
[![codecov](https://codecov.io/gh/metrue/fx/branch/master/graph/badge.svg)](https://codecov.io/gh/metrue/fx) [![codecov](https://codecov.io/gh/metrue/fx/branch/master/graph/badge.svg)](https://codecov.io/gh/metrue/fx)
[![Go Report Card](https://goreportcard.com/badge/github.com/metrue/fx?style=flat-square)](https://goreportcard.com/report/github.com/metrue/fx) [![Go Report Card](https://goreportcard.com/badge/github.com/metrue/fx?style=flat-square)](https://goreportcard.com/report/github.com/metrue/fx)
[![Go Doc](https://img.shields.io/badge/godoc-reference-blue.svg?style=flat-square)](http://godoc.org/github.com/metrue/fx) [![Go Doc](https://img.shields.io/badge/godoc-reference-blue.svg?style=flat-square)](http://godoc.org/github.com/metrue/fx)
@@ -304,33 +305,54 @@ Thank you to all the people who already contributed to fx!
<a href="https://github.com/metrue" target="_blank"> <a href="https://github.com/metrue" target="_blank">
<img alt="metrue" src="https://avatars2.githubusercontent.com/u/1001246?v=4&s=50" width="50"> <img alt="metrue" src="https://avatars2.githubusercontent.com/u/1001246?v=4&s=50" width="50">
</a> </a>
<a href="https://github.com/pplam" target="_blank">
<img alt="pplam" src="https://avatars2.githubusercontent.com/u/12783579?v=4&s=50" width="50">
</a>
<a href="https://github.com/muka" target="_blank"> <a href="https://github.com/muka" target="_blank">
<img alt="muka" src="https://avatars2.githubusercontent.com/u/1021269?v=4&s=50" width="50"> <img alt="muka" src="https://avatars2.githubusercontent.com/u/1021269?v=4&s=50" width="50">
</a> </a>
<a href="https://github.com/xwjdsh" target="_blank"> <a href="https://github.com/pplam" target="_blank">
<img alt="xwjdsh" src="https://avatars2.githubusercontent.com/u/11025519?v=4&s=50" width="50"> <img alt="pplam" src="https://avatars2.githubusercontent.com/u/12783579?v=4&s=50" width="50">
</a> </a>
<a href="https://github.com/mbesancon" target="_blank"> <a href="https://github.com/matbesancon" target="_blank">
<img alt="mbesancon" src="https://avatars2.githubusercontent.com/u/7623090?v=4&s=50" width="50"> <img alt="mbesancon" src="https://avatars2.githubusercontent.com/u/7623090?s=60&v=4" width="50">
</a>
<a href="https://github.com/avelino" target="_blank">
<img alt="avelino" src="https://avatars2.githubusercontent.com/u/31996?v=4&s=50" width="50">
</a>
<a href="https://github.com/DaidoujiChen" target="_blank">
<img alt="DaidoujiChen" src="https://avatars0.githubusercontent.com/u/670441?v=4&s=50" width="50">
</a> </a>
<a href="https://github.com/chlins" target="_blank"> <a href="https://github.com/chlins" target="_blank">
<img alt="chlins" src="https://avatars2.githubusercontent.com/u/31262637?v=4&s=50" width="50"> <img alt="chlins" src="https://avatars2.githubusercontent.com/u/31262637?v=4&s=50" width="50">
</a> </a>
<a href="https://github.com/xwjdsh" target="_blank">
<img alt="xwjdsh" src="https://avatars2.githubusercontent.com/u/11025519?v=4&s=50" width="50">
</a>
<a href="https://github.com/DaidoujiChen" target="_blank">
<img alt="DaidoujiChen" src="https://avatars0.githubusercontent.com/u/670441?v=4&s=50" width="50">
</a>
<a href="https://github.com/avelino" target="_blank">
<img alt="avelino" src="https://avatars2.githubusercontent.com/u/31996?v=4&s=50" width="50">
</a>
<a href="https://github.com/andre2007" target="_blank"> <a href="https://github.com/andre2007" target="_blank">
<img alt="andre2007" src="https://avatars1.githubusercontent.com/u/1451047?s=50&v=4" width="50"> <img alt="andre2007" src="https://avatars1.githubusercontent.com/u/1451047?s=50&v=4" width="50">
</a> </a>
<a href="https://github.com/polyrabbit" target="_blank">
<img alt="polyrabbit" src="https://avatars0.githubusercontent.com/u/2657334?s=60&v=4" width="50">
</a>
<a href="https://github.com/johnlunney" target="_blank">
<img alt="johnlunney" src="https://avatars3.githubusercontent.com/u/536947?s=60&v=4" width="50">
</a>
<a href="https://github.com/tbrand" target="_blank">
<img alt="tbrand" src="https://avatars0.githubusercontent.com/u/3483230?s=60&v=4" width="50">
</a>
<a href="https://github.com/steventhanna" target="_blank"> <a href="https://github.com/steventhanna" target="_blank">
<img alt="andre2007" src="https://avatars1.githubusercontent.com/u/2541678?s=50&v=4" width="50"> <img alt="andre2007" src="https://avatars1.githubusercontent.com/u/2541678?s=50&v=4" width="50">
</a> </a>
<a href="https://github.com/border-radius" target="_blank">
<img alt="border-radius" src="https://avatars0.githubusercontent.com/u/3204785?s=60&v=4" width="50">
</a>
<a href="https://github.com/Russtopia" target="_blank">
<img alt="Russtopia" src="https://avatars1.githubusercontent.com/u/2966177?s=60&v=4<Paste>" width="50">
</a>
<a href="https://github.com/FrontMage" target="_blank">
<img alt="FrontMage" src="https://avatars2.githubusercontent.com/u/17007026?s=60&v=4" width="50">
</a>
<a href="https://github.com/DropNib" target="_blank">
<img alt="DropNib" src="https://avatars0.githubusercontent.com/u/32019589?s=60&v=4" width="50">
</a>
</tr> </tr>
</tbody> </tbody>
</table> </table>

View File

@@ -1,213 +0,0 @@
package config
import (
"fmt"
"os"
"path"
"github.com/spf13/viper"
)
// Configer interface
type Configer interface {
GetMachine(name string) (Host, error)
AddMachine(name string, host Host) error
RemoveHost(name string) error
ListActiveMachines() (map[string]Host, error)
ListMachines() (map[string]Host, error)
EnableMachine(name string) error
DisableMachine(name string) error
UpdateProvisionedStatus(name string, ok bool) error
}
// Config config of fx
type Config struct {
dir string
}
// New create a config
func New(dir string) *Config {
return &Config{dir: dir}
}
// Init config
func (c *Config) Init() error {
if err := os.MkdirAll(c.dir, os.ModePerm); err != nil {
return err
}
ext := "yaml"
name := "config"
viper.SetConfigType(ext)
viper.SetConfigName(name)
viper.AddConfigPath(c.dir)
// detect if file exists
configFilePath := path.Join(c.dir, name+"."+ext)
if _, err := os.Stat(configFilePath); os.IsNotExist(err) {
fd, err := os.Create(configFilePath)
if err != nil {
return err
}
fd.Close()
localhost := Host{
Host: "localhost",
Password: "",
User: "",
Enabled: true,
Provisioned: false,
}
viper.Set("hosts", map[string]Host{"localhost": localhost})
return viper.WriteConfig()
}
if err := viper.ReadInConfig(); err != nil {
return fmt.Errorf("fatal error config file: %s", err)
}
return nil
}
// GetMachine get host by name
func (c *Config) GetMachine(name string) (Host, error) {
var hosts map[string]Host
if err := viper.UnmarshalKey("hosts", &hosts); err != nil {
return Host{}, err
}
host, ok := hosts[name]
if !ok {
return Host{}, fmt.Errorf("no such host %v", name)
}
return host, nil
}
// ListActiveMachines list enabled machines
func (c *Config) ListActiveMachines() (map[string]Host, error) {
hosts, err := c.ListMachines()
if err != nil {
return map[string]Host{}, err
}
lst := map[string]Host{}
for name, h := range hosts {
if h.Enabled {
lst[name] = h
}
}
return lst, nil
}
// AddMachine add host
func (c *Config) AddMachine(name string, host Host) error {
if !viper.IsSet("hosts") {
viper.Set("hosts", map[string]Host{})
}
hosts, err := c.ListMachines()
if err != nil {
return err
}
hosts[name] = host
viper.Set("hosts", hosts)
return viper.WriteConfig()
}
// RemoveHost remote a host
func (c *Config) RemoveHost(name string) error {
hosts, err := c.ListMachines()
if err != nil {
return err
}
if len(hosts) == 1 {
return fmt.Errorf("only one host left now, at least one host required by fx")
}
if _, ok := hosts[name]; ok {
delete(hosts, name)
viper.Set("hosts", hosts)
return viper.WriteConfig()
}
return fmt.Errorf("no such host %s", name)
}
// ListMachines list hosts
func (c *Config) ListMachines() (map[string]Host, error) {
var hosts map[string]Host
if err := viper.UnmarshalKey("hosts", &hosts); err != nil {
return nil, err
}
return hosts, nil
}
// EnableMachine enable a machine, after machine enabled, function will be deployed onto it when ever `fx up` invoked
func (c *Config) EnableMachine(name string) error {
host, err := c.GetMachine(name)
if err != nil {
return err
}
host.Enabled = true
if !viper.IsSet("hosts") {
viper.Set("hosts", map[string]Host{})
}
hosts, err := c.ListMachines()
if err != nil {
return err
}
hosts[name] = host
viper.Set("hosts", hosts)
return viper.WriteConfig()
}
// DisableMachine disable a machine, after machine disabled, function will not be deployed onto it
func (c *Config) DisableMachine(name string) error {
host, err := c.GetMachine(name)
if err != nil {
return err
}
host.Enabled = false
if !viper.IsSet("hosts") {
viper.Set("hosts", map[string]Host{})
}
hosts, err := c.ListMachines()
if err != nil {
return err
}
hosts[name] = host
viper.Set("hosts", hosts)
return viper.WriteConfig()
}
// UpdateProvisionedStatus update provisioned status
func (c *Config) UpdateProvisionedStatus(name string, ok bool) error {
host, err := c.GetMachine(name)
if err != nil {
return err
}
host.Provisioned = ok
if !viper.IsSet("hosts") {
viper.Set("hosts", map[string]Host{})
}
hosts, err := c.ListMachines()
if err != nil {
return err
}
hosts[name] = host
viper.Set("hosts", hosts)
return viper.WriteConfig()
}
// IsMachineProvisioned check if machine provisioned
func (c *Config) IsMachineProvisioned(name string) bool {
host, err := c.GetMachine(name)
if err != nil {
return false
}
return host.Provisioned
}

View File

@@ -1,98 +0,0 @@
package config
import (
"os"
"reflect"
"testing"
)
func TestConfig(t *testing.T) {
configPath := "/tmp/.fx"
defer func() {
if err := os.RemoveAll(configPath); err != nil {
t.Fatal(err)
}
}()
c := New(configPath)
if err := c.Init(); err != nil {
t.Fatal(err)
}
hosts, err := c.ListMachines()
if err != nil {
t.Fatal(err)
}
if len(hosts) != 1 {
t.Fatalf("should have localhost as default machine")
}
host := hosts["localhost"]
if !reflect.DeepEqual(host, Host{Host: "localhost", Enabled: true}) {
t.Fatalf("should get %v but got %v", Host{Host: "localhost"}, host)
}
name := "remote-a"
h := Host{
Host: "192.168.1.1",
User: "user-a",
Password: "password-a",
Enabled: false,
}
if err := c.AddMachine(name, h); err != nil {
t.Fatal(err)
}
hosts, err = c.ListMachines()
if err != nil {
t.Fatal(err)
}
if len(hosts) != 2 {
t.Fatalf("should have %d machines now, but got %d", 2, len(hosts))
}
lst, err := c.ListActiveMachines()
if err != nil {
t.Fatal(err)
}
if len(lst) != 1 {
t.Fatalf("should only have %d machine enabled, but got %d", 1, len(lst))
}
if err := c.EnableMachine(name); err != nil {
t.Fatal(err)
}
lst, err = c.ListActiveMachines()
if err != nil {
t.Fatal(err)
}
if len(lst) != 2 {
t.Fatalf("should only have %d machine enabled, but got %d", 2, len(lst))
}
h.Enabled = true
if !reflect.DeepEqual(lst[name], h) {
t.Fatalf("should get %v but got %v", h, lst[name])
}
if lst[name].Provisioned != false {
t.Fatalf("should get %v but got %v", false, lst[name].Provisioned)
}
if err := c.UpdateProvisionedStatus(name, true); err != nil {
t.Fatal(err)
}
updatedHost, err := c.GetMachine(name)
if err != nil {
t.Fatal(err)
}
if updatedHost.Provisioned != true {
t.Fatalf("should get %v but got %v", true, updatedHost.Provisioned)
}
}

View File

@@ -1,40 +0,0 @@
package config
// Host host entity
type Host struct {
Host string
User string
Password string
Enabled bool
Provisioned bool
}
// NewHost new a host
func NewHost(addr, user, password string) Host {
return Host{
Host: addr,
User: user,
Password: password,
Enabled: false,
Provisioned: false,
}
}
// Valid if host is valid
func (h Host) Valid() bool {
// TODO stronger check
return h.Host != ""
}
// IsLocal if host is localhost
func (h Host) IsLocal() bool {
if !h.Valid() {
return false
}
return h.Host == "127.0.0.1" || h.Host == "localhost"
}
// IsRemote is host is remote
func (h Host) IsRemote() bool {
return !h.IsLocal()
}

View File

@@ -1,149 +0,0 @@
// Code generated by MockGen. DO NOT EDIT.
// Source: ./config.go
// Package mock_config is a generated GoMock package.
package mock_config
import (
gomock "github.com/golang/mock/gomock"
config "github.com/metrue/fx/config"
reflect "reflect"
)
// MockConfiger is a mock of Configer interface
type MockConfiger struct {
ctrl *gomock.Controller
recorder *MockConfigerMockRecorder
}
// MockConfigerMockRecorder is the mock recorder for MockConfiger
type MockConfigerMockRecorder struct {
mock *MockConfiger
}
// NewMockConfiger creates a new mock instance
func NewMockConfiger(ctrl *gomock.Controller) *MockConfiger {
mock := &MockConfiger{ctrl: ctrl}
mock.recorder = &MockConfigerMockRecorder{mock}
return mock
}
// EXPECT returns an object that allows the caller to indicate expected use
func (m *MockConfiger) EXPECT() *MockConfigerMockRecorder {
return m.recorder
}
// GetMachine mocks base method
func (m *MockConfiger) GetMachine(name string) (config.Host, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "GetMachine", name)
ret0, _ := ret[0].(config.Host)
ret1, _ := ret[1].(error)
return ret0, ret1
}
// GetMachine indicates an expected call of GetMachine
func (mr *MockConfigerMockRecorder) GetMachine(name interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetMachine", reflect.TypeOf((*MockConfiger)(nil).GetMachine), name)
}
// AddMachine mocks base method
func (m *MockConfiger) AddMachine(name string, host config.Host) error {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "AddMachine", name, host)
ret0, _ := ret[0].(error)
return ret0
}
// AddMachine indicates an expected call of AddMachine
func (mr *MockConfigerMockRecorder) AddMachine(name, host interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "AddMachine", reflect.TypeOf((*MockConfiger)(nil).AddMachine), name, host)
}
// RemoveHost mocks base method
func (m *MockConfiger) RemoveHost(name string) error {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "RemoveHost", name)
ret0, _ := ret[0].(error)
return ret0
}
// RemoveHost indicates an expected call of RemoveHost
func (mr *MockConfigerMockRecorder) RemoveHost(name interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "RemoveHost", reflect.TypeOf((*MockConfiger)(nil).RemoveHost), name)
}
// ListActiveMachines mocks base method
func (m *MockConfiger) ListActiveMachines() (map[string]config.Host, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "ListActiveMachines")
ret0, _ := ret[0].(map[string]config.Host)
ret1, _ := ret[1].(error)
return ret0, ret1
}
// ListActiveMachines indicates an expected call of ListActiveMachines
func (mr *MockConfigerMockRecorder) ListActiveMachines() *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListActiveMachines", reflect.TypeOf((*MockConfiger)(nil).ListActiveMachines))
}
// ListMachines mocks base method
func (m *MockConfiger) ListMachines() (map[string]config.Host, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "ListMachines")
ret0, _ := ret[0].(map[string]config.Host)
ret1, _ := ret[1].(error)
return ret0, ret1
}
// ListMachines indicates an expected call of ListMachines
func (mr *MockConfigerMockRecorder) ListMachines() *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListMachines", reflect.TypeOf((*MockConfiger)(nil).ListMachines))
}
// EnableMachine mocks base method
func (m *MockConfiger) EnableMachine(name string) error {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "EnableMachine", name)
ret0, _ := ret[0].(error)
return ret0
}
// EnableMachine indicates an expected call of EnableMachine
func (mr *MockConfigerMockRecorder) EnableMachine(name interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "EnableMachine", reflect.TypeOf((*MockConfiger)(nil).EnableMachine), name)
}
// DisableMachine mocks base method
func (m *MockConfiger) DisableMachine(name string) error {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "DisableMachine", name)
ret0, _ := ret[0].(error)
return ret0
}
// DisableMachine indicates an expected call of DisableMachine
func (mr *MockConfigerMockRecorder) DisableMachine(name interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DisableMachine", reflect.TypeOf((*MockConfiger)(nil).DisableMachine), name)
}
// UpdateProvisionedStatus mocks base method
func (m *MockConfiger) UpdateProvisionedStatus(name string, ok bool) error {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "UpdateProvisionedStatus", name, ok)
ret0, _ := ret[0].(error)
return ret0
}
// UpdateProvisionedStatus indicates an expected call of UpdateProvisionedStatus
func (mr *MockConfigerMockRecorder) UpdateProvisionedStatus(name, ok interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpdateProvisionedStatus", reflect.TypeOf((*MockConfiger)(nil).UpdateProvisionedStatus), name, ok)
}

View File

@@ -1,19 +1,32 @@
package api package api
import ( import (
"bufio"
"bytes" "bytes"
"context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"net/url"
"os"
"path/filepath"
"strconv" "strconv"
"strings" "strings"
"time" "time"
"github.com/apex/log"
dockerTypes "github.com/docker/docker/api/types" dockerTypes "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
dockerTypesContainer "github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/network"
"github.com/docker/go-connections/nat"
"github.com/google/go-querystring/query" "github.com/google/go-querystring/query"
"github.com/google/uuid"
containerruntimes "github.com/metrue/fx/container_runtimes"
"github.com/metrue/fx/types" "github.com/metrue/fx/types"
"github.com/metrue/fx/utils" "github.com/metrue/fx/utils"
"github.com/pkg/errors"
) )
// API interact with dockerd http api // API interact with dockerd http api
@@ -116,8 +129,8 @@ func (api *API) post(path string, body []byte, expectStatus int, v interface{})
return nil return nil
} }
// List list service // ListContainer list service
func (api *API) list(name string) ([]types.Service, error) { func (api *API) ListContainer(ctx context.Context, name string) ([]types.Service, error) {
if name != "" { if name != "" {
info, err := api.inspect(name) info, err := api.inspect(name)
if err != nil { if err != nil {
@@ -141,7 +154,7 @@ func (api *API) list(name string) ([]types.Service, error) {
} }
type filterItem struct { type filterItem struct {
Status []string `json:"url,omitempty"` Status []string `json:"status,omitempty"`
Label []string `json:"label,omitempty"` Label []string `json:"label,omitempty"`
Name []string `json:"name,omitempty"` Name []string `json:"name,omitempty"`
} }
@@ -193,3 +206,222 @@ func (api *API) list(name string) ([]types.Service, error) {
return services, nil return services, nil
} }
// BuildImage build image
func (api *API) BuildImage(ctx context.Context, workdir string, name string) error {
tarDir, err := ioutil.TempDir("/tmp", "fx-tar")
if err != nil {
return err
}
defer os.RemoveAll(tarDir)
imageID := uuid.New().String()
tarFilePath := filepath.Join(tarDir, fmt.Sprintf("%s.tar", imageID))
if err := utils.TarDir(workdir, tarFilePath); err != nil {
return err
}
dockerBuildContext, err := os.Open(tarFilePath)
if err != nil {
return err
}
defer dockerBuildContext.Close()
type buildQuery struct {
Labels string `url:"labels,omitempty"`
Tags string `url:"t,omitempty"`
Dockerfile string `url:"dockerfile,omitempty"`
}
// Apply default labels
labelsJSON, _ := json.Marshal(map[string]string{
"belong-to": "fx",
})
q := buildQuery{
Labels: string(labelsJSON),
Dockerfile: "Dockerfile",
}
qs, err := query.Values(q)
if err != nil {
return err
}
qs.Add("t", name)
qs.Add("t", imageID)
path := "/build"
url := fmt.Sprintf("%s%s?%s", api.endpoint, path, qs.Encode())
req, err := http.NewRequest("POST", url, dockerBuildContext)
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/x-tar")
client := &http.Client{Timeout: 600 * time.Second}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
scanner := bufio.NewScanner(resp.Body)
for scanner.Scan() {
if os.Getenv("DEBUG") != "" {
log.Infof(scanner.Text())
}
}
if err := scanner.Err(); err != nil {
return err
}
return nil
}
// PushImage push a image
func (api *API) PushImage(ctx context.Context, name string) (string, error) {
return "", nil
}
// InspectImage inspect image
func (api *API) InspectImage(ctx context.Context, name string, image interface{}) error {
return nil
}
// TagImage tag image
func (api *API) TagImage(ctx context.Context, name string, tag string) error {
query := url.Values{}
query.Set("repo", name)
query.Set("tag", tag)
path := fmt.Sprintf("/images/%s/tag?%s", name, query.Encode())
url := fmt.Sprintf("%s%s", api.endpoint, path)
req, err := http.NewRequest("POST", url, nil)
if err != nil {
return err
}
client := &http.Client{Timeout: 10 * time.Second}
if _, err = client.Do(req); err != nil {
return err
}
return nil
}
// StartContainer start container
func (api *API) StartContainer(ctx context.Context, name string, image string, bindings []types.PortBinding) error {
networks, err := api.GetNetwork(fxNetworkName)
if err != nil {
return errors.Wrapf(err, "get network failed: %s", err)
}
if len(networks) == 0 {
if err := api.CreateNetwork(fxNetworkName); err != nil {
return errors.Wrapf(err, "error create network: %s", err)
}
}
networks, _ = api.GetNetwork(fxNetworkName)
endpoint := &network.EndpointSettings{
NetworkID: networks[0].ID,
}
networkConfig := &network.NetworkingConfig{
EndpointsConfig: map[string]*network.EndpointSettings{
"fx-net": endpoint,
},
}
portSet := nat.PortSet{}
portMap := nat.PortMap{}
for _, binding := range bindings {
bindings := []nat.PortBinding{
nat.PortBinding{
HostIP: types.DefaultHost,
HostPort: fmt.Sprintf("%d", binding.ServiceBindingPort),
},
}
port := nat.Port(fmt.Sprintf("%d/tcp", binding.ContainerExposePort))
portSet[port] = struct{}{}
portMap[port] = bindings
}
config := &dockerTypesContainer.Config{
Image: image,
ExposedPorts: portSet,
}
hostConfig := &dockerTypesContainer.HostConfig{
AutoRemove: true,
PortBindings: portMap,
}
req := ContainerCreateRequestPayload{
Config: config,
HostConfig: hostConfig,
NetworkingConfig: networkConfig,
}
body, err := json.Marshal(req)
if err != nil {
return errors.Wrap(err, "error mashal container create req")
}
// create container
path := fmt.Sprintf("/containers/create?name=%s", name)
var createRes container.ContainerCreateCreatedBody
if err := api.post(path, body, 201, &createRes); err != nil {
return errors.Wrap(err, "create container request failed")
}
if createRes.ID == "" {
return fmt.Errorf("container id is missing")
}
log.Infof("container %s created", name)
// start container
path = fmt.Sprintf("/containers/%s/start", createRes.ID)
url := fmt.Sprintf("%s%s", api.endpoint, path)
request, err := http.NewRequest("POST", url, nil)
if err != nil {
return errors.Wrap(err, "error new container create request")
}
client := &http.Client{Timeout: 20 * time.Second}
resp, err := client.Do(request)
if err != nil {
return errors.Wrap(err, "error do start container request")
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
if len(b) != 0 {
msg := fmt.Sprintf("start container met issue: %s", string(b))
return errors.New(msg)
}
log.Infof("container %s started", name)
if _, err = api.inspect(createRes.ID); err != nil {
msg := fmt.Sprintf("inspect container %s error", name)
return errors.Wrap(err, msg)
}
return nil
}
// StopContainer stop a container
func (api *API) StopContainer(ctx context.Context, name string) error {
return api.Stop(name)
}
// InspectContainer inspect container
func (api *API) InspectContainer(ctx context.Context, name string, container interface{}) error {
return nil
}
var (
_ containerruntimes.ContainerRuntime = &API{}
)

View File

@@ -1,89 +1,111 @@
package api package api
import ( // func TestDockerHTTP(t *testing.T) {
"testing" // const addr = "127.0.0.1"
// const user = ""
"github.com/metrue/fx/config" // const passord = ""
"github.com/metrue/fx/constants" // provisioner := provision.NewWithHost(addr, user, passord)
"github.com/metrue/fx/types" // if err := utils.RunWithRetry(func() error {
) // if !provisioner.IsFxAgentRunning() {
// if err := provisioner.StartFxAgent(); err != nil {
func TestDockerHTTP(t *testing.T) { // log.Infof("could not start fx agent on host: %s", err)
host := config.Host{Host: "127.0.0.1"} // return err
api, err := Create(host.Host, constants.AgentPort) // }
if err != nil { // log.Infof("fx agent started")
t.Fatal(err) // } else {
} // log.Infof("fx agent is running")
// }
serviceName := "a-test-service" // return nil
project := types.Project{ // }, 2*time.Second, 10); err != nil {
Name: serviceName, // t.Fatal(err)
Language: "node", // } else {
Files: []types.ProjectSourceFile{ // defer provisioner.StopFxAgent()
types.ProjectSourceFile{ // }
Path: "Dockerfile", //
Body: ` // host := config.Host{Host: "127.0.0.1"}
FROM metrue/fx-node-base // api, err := Create(host.Host, constants.AgentPort)
// if err != nil {
COPY . . // t.Fatal(err)
EXPOSE 3000 // }
CMD ["node", "app.js"]`, //
IsHandler: false, // serviceName := "a-test-service"
}, // project := types.Project{
types.ProjectSourceFile{ // Name: serviceName,
Path: "app.js", // Language: "node",
Body: ` // Files: []types.ProjectSourceFile{
const Koa = require('koa'); // types.ProjectSourceFile{
const bodyParser = require('koa-bodyparser'); // Path: "Dockerfile",
const func = require('./fx'); // Body: `
// FROM metrue/fx-node-base
const app = new Koa(); //
app.use(bodyParser()); // COPY . .
app.use(ctx => { // EXPOSE 3000
const msg = func(ctx.request.body); // CMD ["node", "app.js"]`,
ctx.body = msg; // IsHandler: false,
}); // },
// types.ProjectSourceFile{
app.listen(3000);`, // Path: "app.js",
IsHandler: false, // Body: `
}, // const Koa = require('koa');
types.ProjectSourceFile{ // const bodyParser = require('koa-bodyparser');
Path: "fx.js", // const func = require('./fx');
Body: ` //
module.exports = (input) => { // const app = new Koa();
return input.a + input.b // app.use(bodyParser());
} // app.use(ctx => {
`, // const msg = func(ctx.request.body);
IsHandler: true, // ctx.body = msg;
}, // });
}, //
} // app.listen(3000);`,
// IsHandler: false,
service, err := api.Build(project) // },
if err != nil { // types.ProjectSourceFile{
t.Fatal(err) // Path: "fx.js",
} // Body: `
// module.exports = (input) => {
if err != nil { // return input.a + input.b
t.Fatal(err) // }
} // `,
if service.Name != serviceName { // IsHandler: true,
t.Fatalf("should get %s but got %s", serviceName, service.Name) // },
} // },
// }
if err := api.Run(9999, &service); err != nil { //
t.Fatal(err) // service, err := api.Build(project)
} // if err != nil {
// t.Fatal(err)
services, err := api.list(serviceName) // }
if err != nil { // if service.Name != serviceName {
t.Fatal(err) // t.Fatalf("should get %s but got %s", serviceName, service.Name)
} // }
if len(services) != 1 { //
t.Fatal("service number should be 1") // if err := api.Run(9999, &service); err != nil {
} // t.Fatal(err)
// }
if err := api.Stop(serviceName); err != nil { //
t.Fatal(err) // services, err := api.ListContainer(serviceName)
} // if err != nil {
} // t.Fatal(err)
// }
// if len(services) != 1 {
// t.Fatal("service number should be 1")
// }
//
// if err := api.Stop(serviceName); err != nil {
// t.Fatal(err)
// }
//
// const network = "fx-net"
// if err := api.CreateNetwork(network); err != nil {
// t.Fatal(err)
// }
//
// nws, err := api.GetNetwork(network)
// if err != nil {
// t.Fatal(err)
// }
// if nws[0].Name != network {
// t.Fatalf("should get %s but got %s", network, nws[0].Name)
// }
// }

View File

@@ -1,60 +1,51 @@
package api package api
import ( import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
"time"
"github.com/apex/log"
"github.com/metrue/fx/types" "github.com/metrue/fx/types"
"github.com/metrue/fx/utils"
) )
// Call function directly with given params // Call function directly with given params
func (api *API) Call(file string, param string, project types.Project) error { func (api *API) Call(file string, param string, project types.Project) error {
service, err := api.Build(project) return nil
if err != nil { // service, err := api.Build(project)
log.Fatalf("Build Service: %v", err) // if err != nil {
return err // log.Fatalf("Build Service: %v", err)
} // return err
log.Info("Build Service: \u2713") // }
// log.Info("Build Service: \u2713")
if err := api.Run(9999, &service); err != nil { //
log.Fatalf("Run Service: %v", err) // if err := api.Run(9999, &service); err != nil {
return err // log.Fatalf("Run Service: %v", err)
} // return err
log.Info("Run Service: \u2713") // }
// log.Info("Run Service: \u2713")
params := utils.PairsToParams(strings.Fields(param)) //
body, err := json.Marshal(params) // params := utils.PairsToParams(strings.Fields(param))
if err != nil { // body, err := json.Marshal(params)
return err // if err != nil {
} // return err
// }
// Wait 2 seconds for service startup //
time.Sleep(time.Second * 2) // // Wait 2 seconds for service startup
// time.Sleep(time.Second * 2)
url := fmt.Sprintf("http://%s:%d", service.Host, service.Port) //
r, err := http.NewRequest("POST", url, bytes.NewReader(body)) // url := fmt.Sprintf("http://%s:%d", service.Host, service.Port)
if err != nil { // r, err := http.NewRequest("POST", url, bytes.NewReader(body))
return err // if err != nil {
} // return err
r.Header.Set("Content-Type", "application/json") // }
client := &http.Client{Timeout: 20 * time.Second} // r.Header.Set("Content-Type", "application/json")
resp, err := client.Do(r) // client := &http.Client{Timeout: 20 * time.Second}
if err != nil { // resp, err := client.Do(r)
log.Fatalf("Call Service: %v", err) // if err != nil {
return err // log.Fatalf("Call Service: %v", err)
} // return err
buf, err := ioutil.ReadAll(resp.Body) // }
if err != nil { // buf, err := ioutil.ReadAll(resp.Body)
log.Fatalf("Call Service: %v", err) // if err != nil {
return err // log.Fatalf("Call Service: %v", err)
} // return err
log.Info("Call Service: \u2713") // }
return utils.OutputJSON(string(buf)) // log.Info("Call Service: \u2713")
// return utils.OutputJSON(string(buf))
} }

View File

@@ -1,22 +0,0 @@
package api
import (
"github.com/apex/log"
"github.com/metrue/fx/utils"
)
// List services
func (api *API) List(name string) error {
services, err := api.list(name)
if err != nil {
log.Fatalf("List Services: %v", err)
return err
}
for _, service := range services {
if err := utils.OutputJSON(service); err != nil {
return err
}
}
return nil
}

View File

@@ -1,35 +0,0 @@
package api
import (
"testing"
"github.com/golang/mock/gomock"
"github.com/metrue/fx/config"
"github.com/metrue/fx/constants"
gock "gopkg.in/h2non/gock.v1"
)
func TestNetwork(t *testing.T) {
defer gock.Off()
ctrl := gomock.NewController(t)
defer ctrl.Finish()
host := config.Host{Host: "127.0.0.1"}
api, err := Create(host.Host, constants.AgentPort)
if err != nil {
t.Fatal(err)
}
const network = "fx-net"
if err := api.CreateNetwork(network); err != nil {
t.Fatal(err)
}
nws, err := api.GetNetwork(network)
if err != nil {
t.Fatal(err)
}
if nws[0].Name != network {
t.Fatalf("should get %s but got %s", network, nws[0].Name)
}
}

View File

@@ -1,119 +1,59 @@
package api package api
import ( // import (
"bufio" // "fmt"
"encoding/json" // "io/ioutil"
"fmt" // "os"
"io/ioutil" // "path/filepath"
"net/http" //
"os" // "github.com/google/uuid"
"path/filepath" // "github.com/metrue/fx/types"
"time" // "github.com/metrue/fx/utils"
// )
"github.com/apex/log" //
"github.com/google/go-querystring/query" // func makeTar(project types.Project, tarFilePath string) error {
"github.com/google/uuid" // dir, err := ioutil.TempDir("/tmp", "fx-build-dir")
"github.com/metrue/fx/types" // if err != nil {
"github.com/metrue/fx/utils" // return err
) // }
//
func makeTar(project types.Project, tarFilePath string) error { // defer os.RemoveAll(dir)
dir, err := ioutil.TempDir("/tmp", "fx-build-dir") //
if err != nil { // for _, file := range project.Files {
return err // tmpfn := filepath.Join(dir, file.Path)
} // if err := utils.EnsureFile(tmpfn); err != nil {
// return err
defer os.RemoveAll(dir) // }
// if err := ioutil.WriteFile(tmpfn, []byte(file.Body), 0666); err != nil {
for _, file := range project.Files { // return err
tmpfn := filepath.Join(dir, file.Path) // }
if err := utils.EnsureFile(tmpfn); err != nil { // }
return err //
} // return utils.TarDir(dir, tarFilePath)
if err := ioutil.WriteFile(tmpfn, []byte(file.Body), 0666); err != nil { // }
return err //
} // // Build build a project
} // func (api *API) Build(project types.Project) (types.Service, error) {
// tarDir, err := ioutil.TempDir("/tmp", "fx-tar")
return utils.TarDir(dir, tarFilePath) // if err != nil {
} // return types.Service{}, err
// }
// Build build a project // defer os.RemoveAll(tarDir)
func (api *API) Build(project types.Project) (types.Service, error) { //
tarDir, err := ioutil.TempDir("/tmp", "fx-tar") // imageID := uuid.New().String()
if err != nil { // tarFilePath := filepath.Join(tarDir, fmt.Sprintf("%s.tar", imageID))
return types.Service{}, err // if err := makeTar(project, tarFilePath); err != nil {
} // return types.Service{}, err
defer os.RemoveAll(tarDir) // }
// labels := map[string]string{
imageID := uuid.New().String() // "belong-to": "fx",
tarFilePath := filepath.Join(tarDir, fmt.Sprintf("%s.tar", imageID)) // }
if err := makeTar(project, tarFilePath); err != nil { // if err := api.BuildImage(tarFilePath, imageID, labels); err != nil {
return types.Service{}, err // return types.Service{}, err
} // }
labels := map[string]string{ //
"belong-to": "fx", // return types.Service{
} // Name: project.Name,
if err := api.BuildImage(tarFilePath, imageID, labels); err != nil { // Image: imageID,
return types.Service{}, err // }, nil
} // }
return types.Service{
Name: project.Name,
Image: imageID,
}, nil
}
// BuildImage build docker image
func (api *API) BuildImage(tarFile string, tag string, labels map[string]string) error {
dockerBuildContext, err := os.Open(tarFile)
if err != nil {
return err
}
defer dockerBuildContext.Close()
type buildQuery struct {
Labels string `url:"labels,omitempty"`
Tags string `url:"t,omitempty"`
Dockerfile string `url:"dockerfile,omitempty"`
}
// Apply default labels
labelsJSON, _ := json.Marshal(labels)
q := buildQuery{
Tags: tag,
Labels: string(labelsJSON),
Dockerfile: "Dockerfile",
}
qs, err := query.Values(q)
if err != nil {
return err
}
path := "/build"
url := fmt.Sprintf("%s%s?%s", api.endpoint, path, qs.Encode())
req, err := http.NewRequest("POST", url, dockerBuildContext)
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/x-tar")
client := &http.Client{Timeout: 600 * time.Second}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
scanner := bufio.NewScanner(resp.Body)
for scanner.Scan() {
if os.Getenv("DEBUG") != "" {
log.Infof(scanner.Text())
}
}
if err := scanner.Err(); err != nil {
return err
}
return nil
}

View File

@@ -1,166 +1,82 @@
package api package api
import ( // import (
"fmt" // "fmt"
"io/ioutil" // "io/ioutil"
"net/http" // "os"
"os" // "path/filepath"
"path/filepath" // "testing"
"strings" //
"testing" // "github.com/metrue/fx/types"
// )
"github.com/metrue/fx/config" //
"github.com/metrue/fx/constants" // func TestMakeTar(t *testing.T) {
"github.com/metrue/fx/types" // serviceName := "mock-service-abc"
gock "gopkg.in/h2non/gock.v1" // project := types.Project{
) // Name: serviceName,
// Language: "node",
func TestMakeTar(t *testing.T) { // Files: []types.ProjectSourceFile{
serviceName := "mock-service-abc" // types.ProjectSourceFile{
project := types.Project{ // Path: "Dockerfile",
Name: serviceName, // Body: `
Language: "node", // FROM metrue/fx-node-base
Files: []types.ProjectSourceFile{ //
types.ProjectSourceFile{ // COPY . .
Path: "Dockerfile", // EXPOSE 3000
Body: ` // CMD ["node", "app.js"]`,
FROM metrue/fx-node-base // IsHandler: false,
// },
COPY . . // types.ProjectSourceFile{
EXPOSE 3000 // Path: "app.js",
CMD ["node", "app.js"]`, // Body: `
IsHandler: false, // const Koa = require('koa');
}, // const bodyParser = require('koa-bodyparser');
types.ProjectSourceFile{ // const func = require('./fx');
Path: "app.js", //
Body: ` // const app = new Koa();
const Koa = require('koa'); // app.use(bodyParser());
const bodyParser = require('koa-bodyparser'); // app.use(ctx => {
const func = require('./fx'); // const msg = func(ctx.request.body);
// ctx.body = msg;
const app = new Koa(); // });
app.use(bodyParser()); //
app.use(ctx => { // app.listen(3000);`,
const msg = func(ctx.request.body); // IsHandler: false,
ctx.body = msg; // },
}); // types.ProjectSourceFile{
// Path: "fx.js",
app.listen(3000);`, // Body: `
IsHandler: false, // module.exports = (input) => {
}, // return input.a + input.b
types.ProjectSourceFile{ // }
Path: "fx.js", // `,
Body: ` // IsHandler: true,
module.exports = (input) => { // },
return input.a + input.b // },
} // }
`, // tarDir, err := ioutil.TempDir("/tmp", "fx-tar")
IsHandler: true, // if err != nil {
}, // t.Fatal(err)
}, // }
} // defer os.RemoveAll(tarDir)
tarDir, err := ioutil.TempDir("/tmp", "fx-tar") //
if err != nil { // tarFilePath := filepath.Join(tarDir, fmt.Sprintf("%s.tar", serviceName))
t.Fatal(err) // if err := makeTar(project, tarFilePath); err != nil {
} // t.Fatal(err)
defer os.RemoveAll(tarDir) // }
//
tarFilePath := filepath.Join(tarDir, fmt.Sprintf("%s.tar", serviceName)) // file, err := os.Open(tarFilePath)
if err := makeTar(project, tarFilePath); err != nil { // if err != nil {
t.Fatal(err) // t.Fatal(err)
} // }
// stat, err := file.Stat()
file, err := os.Open(tarFilePath) // if err != nil {
if err != nil { // t.Fatal(err)
t.Fatal(err) // }
} // if stat.Name() != serviceName+".tar" {
stat, err := file.Stat() // t.Fatalf("should get %s but got %s", serviceName+".tar", stat.Name())
if err != nil { // }
t.Fatal(err) // if stat.Size() <= 0 {
} // t.Fatalf("tarfile invalid: size %d", stat.Size())
if stat.Name() != serviceName+".tar" { // }
t.Fatalf("should get %s but got %s", serviceName+".tar", stat.Name()) // }
}
if stat.Size() <= 0 {
t.Fatalf("tarfile invalid: size %d", stat.Size())
}
}
func TestBuild(t *testing.T) {
defer gock.Off()
host := config.Host{Host: "127.0.0.1"}
api, err := Create(host.Host, constants.AgentPort)
if err != nil {
t.Fatal(err)
}
url := "http://" + host.Host + ":" + constants.AgentPort
gock.New(url).
Post("/v" + api.version + "/build").
AddMatcher(func(req *http.Request, ereq *gock.Request) (bool, error) {
if strings.Contains(req.URL.String(), "/v"+api.version+"/build") {
return true, nil
}
return false, nil
}).
Reply(200).
JSON(map[string]string{
"stream": "Step 1/5...",
})
serviceName := "mock-service-abc"
project := types.Project{
Name: serviceName,
Language: "node",
Files: []types.ProjectSourceFile{
types.ProjectSourceFile{
Path: "Dockerfile",
Body: `
FROM metrue/fx-node-base
COPY . .
EXPOSE 3000
CMD ["node", "app.js"]`,
IsHandler: false,
},
types.ProjectSourceFile{
Path: "app.js",
Body: `
const Koa = require('koa');
const bodyParser = require('koa-bodyparser');
const func = require('./fx');
const app = new Koa();
app.use(bodyParser());
app.use(ctx => {
const msg = func(ctx.request.body);
ctx.body = msg;
});
app.listen(3000);`,
IsHandler: false,
},
types.ProjectSourceFile{
Path: "fx.js",
Body: `
module.exports = (input) => {
return input.a + input.b
}
`,
IsHandler: true,
},
},
}
service, err := api.Build(project)
if err != nil {
t.Fatal(err)
}
if service.Name != serviceName {
t.Fatalf("should get %s but got %s", serviceName, service.Name)
}
if service.Image == "" {
t.Fatal("service image should not be empty")
}
}

View File

@@ -1,55 +0,0 @@
package api
import (
"net/http"
"testing"
"github.com/golang/mock/gomock"
"github.com/metrue/fx/config"
"github.com/metrue/fx/constants"
"github.com/metrue/fx/types"
gock "gopkg.in/h2non/gock.v1"
)
func TestServiceRun(t *testing.T) {
defer gock.Off()
ctrl := gomock.NewController(t)
defer ctrl.Finish()
host := config.Host{Host: "127.0.0.1"}
api, err := Create(host.Host, constants.AgentPort)
if err != nil {
t.Fatal(err)
}
service := types.Service{
Name: "a-mock-service",
Image: "a-mock-image-id",
}
mockContainerID := "mock-container-id"
url := "http://" + host.Host + ":" + constants.AgentPort
gock.New(url).
Post("/v0.2.1/containers").
AddMatcher(func(req *http.Request, ereq *gock.Request) (m bool, e error) {
// TODO multiple matching not supported by gock
if req.URL.String() == url+"/v0.2.1/containers/"+mockContainerID+"/start" {
return true, nil
} else if req.URL.String() == url+"/v0.2.1/containers/create?name="+service.Name {
return true, nil
}
return false, nil
}).
Reply(201).
JSON(map[string]interface{}{
"Id": mockContainerID,
"Warnings": []string{},
})
// FIXME
if err := api.Run(9999, &service); err == nil {
t.Fatal(err)
}
}

View File

@@ -1,39 +0,0 @@
package api
import (
"net/http"
"strings"
"testing"
"github.com/golang/mock/gomock"
"github.com/metrue/fx/config"
"github.com/metrue/fx/constants"
gock "gopkg.in/h2non/gock.v1"
)
func TestStop(t *testing.T) {
defer gock.Off()
ctrl := gomock.NewController(t)
defer ctrl.Finish()
host := config.Host{Host: "127.0.0.1"}
api, err := Create(host.Host, constants.AgentPort)
if err != nil {
t.Fatal(err)
}
mockServiceName := "mock-service-name"
url := "http://" + host.Host + ":" + constants.AgentPort
gock.New(url).
Post("/v" + api.version + "/containers/" + mockServiceName + "/stop").
AddMatcher(func(req *http.Request, ereq *gock.Request) (m bool, e error) {
if strings.Contains(req.URL.String(), "/v"+api.version+"/containers/"+mockServiceName+"/stop") {
return true, nil
}
return false, nil
}).
Reply(204)
if err := api.Stop(mockServiceName); err != nil {
t.Fatal(err)
}
}

View File

@@ -1,81 +1,71 @@
package api package api
import ( // // UpOptions options for up
"context" // type UpOptions struct {
"time" // Body []byte
// Lang string
"github.com/apex/log" // Name string
"github.com/docker/docker/api/types/container" // Port int
"github.com/metrue/fx/constants" // HealtCheck bool
"github.com/metrue/fx/types" // Project types.Project
) // }
//
// UpOptions options for up // // Up up a source code of function to be a service
type UpOptions struct { // func (api *API) Up(opt UpOptions) error {
Body []byte // service, err := api.Build(opt.Project)
Lang string // if err != nil {
Name string // log.Fatalf("Build Service %s: %v", opt.Name, err)
Port int // return err
HealtCheck bool // }
Project types.Project // log.Infof("Build Service %s: %s", opt.Name, constants.CheckedSymbol)
} //
// if err := api.Run(opt.Port, &service); err != nil {
// Up up a source code of function to be a service // log.Fatalf("Run Service: %v", err)
func (api *API) Up(opt UpOptions) error { // return err
service, err := api.Build(opt.Project) // }
if err != nil { // log.Infof("Run Service: %s", constants.CheckedSymbol)
log.Fatalf("Build Service %s: %v", opt.Name, err) // log.Infof("Service (%s) is running on: %s:%d", service.Name, service.Host, service.Port)
return err //
} // if opt.HealtCheck {
log.Infof("Build Service %s: %s", opt.Name, constants.CheckedSymbol) // go func() {
// resultC, errC := api.ContainerWait(
if err := api.Run(opt.Port, &service); err != nil { // context.Background(),
log.Fatalf("Run Service: %v", err) // service.ID,
return err // container.WaitConditionNextExit,
} // 20*time.Second,
log.Infof("Run Service: %s", constants.CheckedSymbol) // )
log.Infof("Service (%s) is running on: %s:%d", service.Name, service.Host, service.Port) // for {
// select {
if opt.HealtCheck { // case res := <-resultC:
go func() { // var msg string
resultC, errC := api.ContainerWait( // if res.Error != nil {
context.Background(), // msg = res.Error.Message
service.ID, // }
container.WaitConditionNextExit, // log.Warnf("container exited: Code(%d) %s %s", res.StatusCode, msg, constants.UncheckedSymbol)
20*time.Second, // case err := <-errC:
) // log.Fatalf("wait container status exit: %s, %v", constants.UncheckedSymbol, err)
for { // }
select { // }
case res := <-resultC: // }()
var msg string //
if res.Error != nil { // trys := 0
msg = res.Error.Message // for {
} // if trys > 2 {
log.Warnf("container exited: Code(%d) %s %s", res.StatusCode, msg, constants.UncheckedSymbol) // break
case err := <-errC: // }
log.Fatalf("wait container status exit: %s, %v", constants.UncheckedSymbol, err) // info, err := api.inspect(service.ID)
} // if err != nil {
} // log.Fatalf("healt checking failed: %v", err)
}() // }
// if info.State.Running {
trys := 0 // log.Info("service is running")
for { // } else {
if trys > 2 { // log.Warnf("service is %s", info.State.Status)
break // }
} // time.Sleep(1 * time.Second)
info, err := api.inspect(service.ID) // trys++
if err != nil { // }
log.Fatalf("healt checking failed: %v", err) // }
} //
if info.State.Running { // return nil
log.Info("service is running") // }
} else {
log.Warnf("service is %s", info.State.Status)
}
time.Sleep(1 * time.Second)
trys++
}
}
return nil
}

View File

@@ -9,10 +9,12 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"strings"
"github.com/apex/log" "github.com/apex/log"
dockerTypes "github.com/docker/docker/api/types" dockerTypes "github.com/docker/docker/api/types"
dockerTypesContainer "github.com/docker/docker/api/types/container" dockerTypesContainer "github.com/docker/docker/api/types/container"
dockerFilters "github.com/docker/docker/api/types/filters"
"github.com/docker/docker/client" "github.com/docker/docker/client"
"github.com/docker/go-connections/nat" "github.com/docker/go-connections/nat"
"github.com/google/uuid" "github.com/google/uuid"
@@ -70,14 +72,11 @@ func (d *Docker) BuildImage(ctx context.Context, workdir string, name string) er
return err return err
} }
defer resp.Body.Close() defer resp.Body.Close()
if os.Getenv("DEBUG") != "" {
body, err := ioutil.ReadAll(resp.Body) body, err := ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
return err return err
} }
log.Info(string(body)) log.Info(string(body))
}
return nil return nil
} }
@@ -134,6 +133,11 @@ func (d *Docker) InspectImage(ctx context.Context, name string, img interface{})
return json.NewDecoder(rdr).Decode(&img) return json.NewDecoder(rdr).Decode(&img)
} }
// TagImage tag image
func (d *Docker) TagImage(ctx context.Context, name string, tag string) error {
return d.ImageTag(ctx, name, tag)
}
// StartContainer create and start a container from given image // StartContainer create and start a container from given image
func (d *Docker) StartContainer(ctx context.Context, name string, image string, ports []types.PortBinding) error { func (d *Docker) StartContainer(ctx context.Context, name string, image string, ports []types.PortBinding) error {
portSet := nat.PortSet{} portSet := nat.PortSet{}
@@ -186,6 +190,40 @@ func (d *Docker) InspectContainer(ctx context.Context, name string, container in
return nil return nil
} }
// ListContainer list containers
func (d *Docker) ListContainer(ctx context.Context, name string) ([]types.Service, error) {
args := dockerFilters.NewArgs(
dockerFilters.Arg("label", "belong-to=fx"),
)
containers, err := d.ContainerList(ctx, dockerTypes.ContainerListOptions{
Filters: args,
})
if err != nil {
return []types.Service{}, err
}
svs := make(map[string]types.Service)
for _, container := range containers {
// container name have extra forward slash
// https://github.com/moby/moby/issues/6705
if strings.HasPrefix(container.Names[0], fmt.Sprintf("/%s", name)) {
svs[container.Image] = types.Service{
Name: container.Names[0],
Image: container.Image,
ID: container.ID,
Host: container.Ports[0].IP,
Port: int(container.Ports[0].PublicPort),
State: container.State,
}
}
}
services := []types.Service{}
for _, s := range svs {
services = append(services, s)
}
return services, nil
}
var ( var (
_ containerruntimes.ContainerRuntime = &Docker{} _ containerruntimes.ContainerRuntime = &Docker{}
) )

View File

@@ -10,8 +10,10 @@ import (
type ContainerRuntime interface { type ContainerRuntime interface {
BuildImage(ctx context.Context, workdir string, name string) error BuildImage(ctx context.Context, workdir string, name string) error
PushImage(ctx context.Context, name string) (string, error) PushImage(ctx context.Context, name string) (string, error)
InspectImage(ct context.Context, name string, img interface{}) error InspectImage(ctx context.Context, name string, img interface{}) error
TagImage(ctx context.Context, name string, tag string) error
StartContainer(ctx context.Context, name string, image string, bindings []types.PortBinding) error StartContainer(ctx context.Context, name string, image string, bindings []types.PortBinding) error
StopContainer(ctx context.Context, name string) error StopContainer(ctx context.Context, name string) error
InspectContainer(ctx context.Context, name string, container interface{}) error InspectContainer(ctx context.Context, name string, container interface{}) error
ListContainer(ctx context.Context, filter string) ([]types.Service, error)
} }

58
context/context.go Normal file
View File

@@ -0,0 +1,58 @@
package context
import (
"context"
"github.com/urfave/cli"
)
type key string
const (
keyCliCtx = key("cmd_cli")
)
// Context fx context
type Context struct {
context.Context
}
// NewContext new a context
func NewContext() *Context {
ctx := context.Background()
return &Context{ctx}
}
// FromCliContext create context from cli.Context
func FromCliContext(c *cli.Context) *Context {
ctx := NewContext()
ctx.WithCliContext(c)
return ctx
}
// WithCliContext set cli.Context
func (ctx *Context) WithCliContext(c *cli.Context) {
newCtx := context.WithValue(ctx.Context, keyCliCtx, c)
ctx.Context = newCtx
}
// GetCliContext get cli.Context
func (ctx *Context) GetCliContext() *cli.Context {
return ctx.Value(keyCliCtx).(*cli.Context)
}
// Set a value with name
func (ctx *Context) Set(name string, value interface{}) {
newCtx := context.WithValue(ctx.Context, name, value)
ctx.Context = newCtx
}
// Get a value
func (ctx *Context) Get(name string) interface{} {
return ctx.Context.Value(name)
}
// Use invole a middle
func (ctx *Context) Use(fn func(ctx *Context) error) error {
return fn(ctx)
}

25
context/context_test.go Normal file
View File

@@ -0,0 +1,25 @@
package context
import (
"testing"
"github.com/urfave/cli"
)
func TestContext(t *testing.T) {
ctx := NewContext()
cli := cli.NewContext(nil, nil, nil)
ctx.WithCliContext(cli)
c := ctx.GetCliContext()
if c != cli {
t.Fatalf("should get %v but got %v", cli, c)
}
key := "k_1"
value := "hello"
ctx.Set(key, "hello")
v := ctx.Get(key).(string)
if v != value {
t.Fatalf("should get %v but %v", value, v)
}
}

View File

@@ -15,9 +15,6 @@ import (
"github.com/metrue/fx/utils" "github.com/metrue/fx/utils"
) )
// Version binary version
var Version = "0.0.1"
func init() { func init() {
// TODO clean it up // TODO clean it up
os.Setenv("DEBUG", "true") os.Setenv("DEBUG", "true")
@@ -43,6 +40,7 @@ docker_packer <encrypt_docker_project_source_tree> <image_name>
} }
var tree map[string]string var tree map[string]string
//nolint
if err := json.Unmarshal([]byte(str), &tree); err != nil { if err := json.Unmarshal([]byte(str), &tree); err != nil {
log.Fatalf("could not unmarshal meta: %s", meta) log.Fatalf("could not unmarshal meta: %s", meta)
os.Exit(1) os.Exit(1)

View File

@@ -12,4 +12,5 @@ type Deployer interface {
Destroy(ctx context.Context, name string) error Destroy(ctx context.Context, name string) error
Update(ctx context.Context, name string) error Update(ctx context.Context, name string) error
GetStatus(ctx context.Context, name string) error GetStatus(ctx context.Context, name string) error
List(ctx context.Context, name string) ([]types.Service, error)
} }

View File

@@ -8,7 +8,10 @@ import (
"time" "time"
dockerTypes "github.com/docker/docker/api/types" dockerTypes "github.com/docker/docker/api/types"
runtime "github.com/metrue/fx/container_runtimes/docker/sdk" "github.com/metrue/fx/constants"
containerruntimes "github.com/metrue/fx/container_runtimes"
dockerHTTP "github.com/metrue/fx/container_runtimes/docker/http"
dockerSDK "github.com/metrue/fx/container_runtimes/docker/sdk"
"github.com/metrue/fx/deploy" "github.com/metrue/fx/deploy"
"github.com/metrue/fx/packer" "github.com/metrue/fx/packer"
"github.com/metrue/fx/types" "github.com/metrue/fx/types"
@@ -17,16 +20,27 @@ import (
// Docker manage container // Docker manage container
type Docker struct { type Docker struct {
client *runtime.Docker cli containerruntimes.ContainerRuntime
} }
// CreateClient create a docker instance // CreateClient create a docker instance
func CreateClient(ctx context.Context) (*Docker, error) { func CreateClient(ctx context.Context) (d *Docker, err error) {
cli, err := runtime.CreateClient(ctx) var cli containerruntimes.ContainerRuntime
host := os.Getenv("DOCKER_REMOTE_HOST_ADDR")
user := os.Getenv("DOCKER_REMOTE_HOST_USER")
if host != "" && user != "" {
cli, err = dockerHTTP.Create(host, constants.AgentPort)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &Docker{client: cli}, nil } else {
cli, err = dockerSDK.CreateClient(ctx)
if err != nil {
return nil, err
}
}
return &Docker{cli: cli}, nil
} }
// Deploy create a Docker container from given image, and bind the constants.FxContainerExposePort to given port // Deploy create a Docker container from given image, and bind the constants.FxContainerExposePort to given port
@@ -35,16 +49,18 @@ func (d *Docker) Deploy(ctx context.Context, fn types.Func, name string, ports [
defer os.RemoveAll(workdir) defer os.RemoveAll(workdir)
if err := packer.PackIntoDir(fn, workdir); err != nil { if err := packer.PackIntoDir(fn, workdir); err != nil {
log.Fatalf("could not pack function %v: %v", fn, err)
return err return err
} }
if err := d.client.BuildImage(ctx, workdir, name); err != nil { if err := d.cli.BuildImage(ctx, workdir, name); err != nil {
log.Fatalf("could not build image: %v", err)
return err return err
} }
nameWithTag := name + ":latest" nameWithTag := name + ":latest"
if err := d.client.ImageTag(ctx, name, nameWithTag); err != nil { if err := d.cli.TagImage(ctx, name, nameWithTag); err != nil {
log.Fatalf("could tag image: %v", err) log.Fatalf("could not tag image: %v", err)
return err return err
} }
@@ -54,12 +70,12 @@ func (d *Docker) Deploy(ctx context.Context, fn types.Func, name string, ports [
// But it takes some times waiting image ready after image built, we retry to make sure it ready here // But it takes some times waiting image ready after image built, we retry to make sure it ready here
var imgInfo dockerTypes.ImageInspect var imgInfo dockerTypes.ImageInspect
if err := utils.RunWithRetry(func() error { if err := utils.RunWithRetry(func() error {
return d.client.InspectImage(ctx, name, &imgInfo) return d.cli.InspectImage(ctx, name, &imgInfo)
}, time.Second*1, 5); err != nil { }, time.Second*1, 5); err != nil {
return err return err
} }
return d.client.StartContainer(ctx, name, name, ports) return d.cli.StartContainer(ctx, name, name, ports)
} }
// Update a container // Update a container
@@ -69,7 +85,7 @@ func (d *Docker) Update(ctx context.Context, name string) error {
// Destroy stop and remove container // Destroy stop and remove container
func (d *Docker) Destroy(ctx context.Context, name string) error { func (d *Docker) Destroy(ctx context.Context, name string) error {
return d.client.ContainerStop(ctx, name, nil) return d.cli.StopContainer(ctx, name)
} }
// GetStatus get status of container // GetStatus get status of container
@@ -77,6 +93,12 @@ func (d *Docker) GetStatus(ctx context.Context, name string) error {
return nil return nil
} }
// List services
func (d *Docker) List(ctx context.Context, name string) ([]types.Service, error) {
// FIXME support remote host
return d.cli.ListContainer(ctx, name)
}
var ( var (
_ deploy.Deployer = &Docker{} _ deploy.Deployer = &Docker{}
) )

View File

@@ -131,6 +131,11 @@ func (k *K8S) GetStatus(ctx context.Context, name string) error {
return nil return nil
} }
// List services
func (k *K8S) List(ctx context.Context, name string) ([]types.Service, error) {
return []types.Service{}, nil
}
var ( var (
_ deploy.Deployer = &K8S{} _ deploy.Deployer = &K8S{}
) )

16
docs/lightsail.yml Normal file
View File

@@ -0,0 +1,16 @@
# fx on Amazon Lightsai
* make sure your instance have docker installed and running,
* make sure your instance can be ssh login (with user and password)
```
ssh <user>@<host>
```
* make sure your instance accept port 8866
* then you can deploy function to remote host
```
DOCKER_REMOTE_HOST_ADDR=<your host> DOCKER_REMOTE_HOST_USER=<your user> DOCKER_REMOTE_HOST_PASSWORD=<your password> ./build/fx up -p 1234 test/functions/func.js
```

View File

@@ -2,7 +2,6 @@ package doctor
import ( import (
"github.com/apex/log" "github.com/apex/log"
"github.com/metrue/fx/config"
"github.com/metrue/fx/constants" "github.com/metrue/fx/constants"
"github.com/metrue/fx/pkg/command" "github.com/metrue/fx/pkg/command"
"github.com/metrue/go-ssh-client" "github.com/metrue/go-ssh-client"
@@ -10,16 +9,23 @@ import (
// Doctor health checking // Doctor health checking
type Doctor struct { type Doctor struct {
host config.Host host string
sshClient ssh.Client sshClient ssh.Client
} }
func isLocal(host string) bool {
if host == "" {
return false
}
return host == "127.0.0.1" || host == "localhost" || host == "0.0.0.0"
}
// New a doctor // New a doctor
func New(host config.Host) *Doctor { func New(host, user, password string) *Doctor {
sshClient := ssh.New(host.Host). sshClient := ssh.New(host).
WithUser(host.User). WithUser(user).
WithPassword(host.Password) WithPassword(password)
return &Doctor{ return &Doctor{
host: host, host: host,
sshClient: sshClient, sshClient: sshClient,
@@ -32,7 +38,7 @@ func (d *Doctor) Start() error {
checkAgent := "docker inspect " + constants.AgentContainerName checkAgent := "docker inspect " + constants.AgentContainerName
cmds := []*command.Command{} cmds := []*command.Command{}
if d.host.IsRemote() { if !isLocal(d.host) {
cmds = append(cmds, cmds = append(cmds,
command.New("check if dockerd is running", checkDocker, command.NewRemoteRunner(d.sshClient)), command.New("check if dockerd is running", checkDocker, command.NewRemoteRunner(d.sshClient)),
command.New("check if fx agent is running", checkAgent, command.NewRemoteRunner(d.sshClient)), command.New("check if fx agent is running", checkAgent, command.NewRemoteRunner(d.sshClient)),

142
fx.go
View File

@@ -5,29 +5,20 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"os" "os"
"path"
"regexp" "regexp"
"github.com/apex/log" "github.com/apex/log"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/metrue/fx/config" "github.com/metrue/fx/context"
"github.com/metrue/fx/handlers" "github.com/metrue/fx/handlers"
"github.com/metrue/fx/middlewares"
"github.com/urfave/cli" "github.com/urfave/cli"
) )
const version = "0.7.5" const version = "0.8.0"
var cfg *config.Config
func init() { func init() {
go checkForUpdate() go checkForUpdate()
configDir := path.Join(os.Getenv("HOME"), ".fx")
cfg := config.New(configDir)
if err := cfg.Init(); err != nil {
log.Fatalf("Init config failed %s", err)
os.Exit(1)
}
} }
func checkForUpdate() { func checkForUpdate() {
@@ -67,63 +58,10 @@ func main() {
app.Commands = []cli.Command{ app.Commands = []cli.Command{
{ {
Name: "infra", Name: "init",
Usage: "manage infrastructure of fx", Usage: "start fx agent on host",
Subcommands: []cli.Command{
{
Name: "add",
Usage: "add a new machine",
Flags: []cli.Flag{
cli.StringFlag{
Name: "name, N",
Usage: "a alias name for this machine",
},
cli.StringFlag{
Name: "host, H",
Usage: "host name or IP address of a machine",
},
cli.StringFlag{
Name: "user, U",
Usage: "user name required for SSH login",
},
cli.StringFlag{
Name: "password, P",
Usage: "password required for SSH login",
},
},
Action: func(c *cli.Context) error { Action: func(c *cli.Context) error {
return handlers.AddHost(cfg)(c) return handlers.Init()(context.FromCliContext(c))
},
},
{
Name: "remove",
Usage: "remove an existing machine",
Action: func(c *cli.Context) error {
return handlers.RemoveHost(cfg)(c)
},
},
{
Name: "list",
Aliases: []string{"ls"},
Usage: "list machines",
Action: func(c *cli.Context) error {
return handlers.ListHosts(cfg)(c)
},
},
{
Name: "activate",
Usage: "enable a machine be a host of fx infrastructure",
Action: func(c *cli.Context) error {
return handlers.Activate(cfg)(c)
},
},
{
Name: "deactivate",
Usage: "disable a machine be a host of fx infrastructure",
Action: func(c *cli.Context) error {
return handlers.Deactivate(cfg)(c)
},
},
}, },
}, },
{ {
@@ -140,7 +78,11 @@ func main() {
}, },
}, },
Action: func(c *cli.Context) error { Action: func(c *cli.Context) error {
return handlers.BuildImage(cfg)(c) ctx := context.FromCliContext(c)
if err := ctx.Use(middlewares.Setup); err != nil {
log.Fatalf("%v", err)
}
return handlers.BuildImage()(ctx)
}, },
}, },
{ {
@@ -153,7 +95,7 @@ func main() {
}, },
}, },
Action: func(c *cli.Context) error { Action: func(c *cli.Context) error {
return handlers.ExportImage()(c) return handlers.ExportImage()(context.FromCliContext(c))
}, },
}, },
}, },
@@ -162,7 +104,7 @@ func main() {
Name: "doctor", Name: "doctor",
Usage: "health check for fx", Usage: "health check for fx",
Action: func(c *cli.Context) error { Action: func(c *cli.Context) error {
return handlers.Doctor(cfg)(c) return handlers.Doctor()(context.FromCliContext(c))
}, },
}, },
{ {
@@ -189,34 +131,14 @@ func main() {
}, },
}, },
Action: func(c *cli.Context) error { Action: func(c *cli.Context) error {
return handlers.Up(cfg)(c) ctx := context.FromCliContext(c)
}, if err := ctx.Use(middlewares.Setup); err != nil {
}, log.Fatalf("%v", err)
{ }
Name: "deploy", if err := ctx.Use(middlewares.Binding); err != nil {
Usage: "deploy a function or a group of functions", log.Fatalf("%v", err)
ArgsUsage: "[func.go func.js func.py func.rb ...]", }
Flags: []cli.Flag{ return handlers.Up()(ctx)
cli.StringFlag{
Name: "name, n",
Value: uuid.New().String(),
Usage: "service name",
},
cli.IntFlag{
Name: "port, p",
Usage: "port number",
},
cli.BoolFlag{
Name: "healthcheck, hc",
Usage: "do a health check after service up",
},
cli.BoolFlag{
Name: "force, f",
Usage: "force deploy a function or functions",
},
},
Action: func(c *cli.Context) error {
return handlers.Deploy(cfg)(c)
}, },
}, },
{ {
@@ -224,15 +146,11 @@ func main() {
Usage: "destroy a service", Usage: "destroy a service",
ArgsUsage: "[service 1, service 2, ....]", ArgsUsage: "[service 1, service 2, ....]",
Action: func(c *cli.Context) error { Action: func(c *cli.Context) error {
return handlers.Down(cfg)(c) ctx := context.FromCliContext(c)
}, if err := ctx.Use(middlewares.Setup); err != nil {
}, log.Fatalf("%v", err)
{ }
Name: "destroy", return handlers.Down()(ctx)
Usage: "destroy a service",
ArgsUsage: "[service 1, service 2, ....]",
Action: func(c *cli.Context) error {
return handlers.Destroy(cfg)(c)
}, },
}, },
{ {
@@ -240,7 +158,11 @@ func main() {
Aliases: []string{"ls"}, Aliases: []string{"ls"},
Usage: "list deployed services", Usage: "list deployed services",
Action: func(c *cli.Context) error { Action: func(c *cli.Context) error {
return handlers.List(cfg)(c) ctx := context.FromCliContext(c)
if err := ctx.Use(middlewares.Setup); err != nil {
log.Fatalf("%v", err)
}
return handlers.List()(ctx)
}, },
}, },
{ {
@@ -253,7 +175,7 @@ func main() {
}, },
}, },
Action: func(c *cli.Context) error { Action: func(c *cli.Context) error {
return handlers.Call(cfg)(c) return handlers.Call()(context.FromCliContext(c))
}, },
}, },
} }

3
go.mod
View File

@@ -20,7 +20,6 @@ require (
github.com/googleapis/gnostic v0.3.1 // indirect github.com/googleapis/gnostic v0.3.1 // indirect
github.com/gorilla/mux v1.7.3 // indirect github.com/gorilla/mux v1.7.3 // indirect
github.com/imdario/mergo v0.3.7 // indirect github.com/imdario/mergo v0.3.7 // indirect
github.com/magiconair/properties v1.8.1 // indirect
github.com/metrue/go-ssh-client v0.0.0-20190810064746-98a7a27048f3 github.com/metrue/go-ssh-client v0.0.0-20190810064746-98a7a27048f3
github.com/mholt/archiver v3.1.1+incompatible github.com/mholt/archiver v3.1.1+incompatible
github.com/morikuni/aec v1.0.0 // indirect github.com/morikuni/aec v1.0.0 // indirect
@@ -31,7 +30,7 @@ require (
github.com/pierrec/lz4 v0.0.0-20190222153722-062282ea0dcf // indirect github.com/pierrec/lz4 v0.0.0-20190222153722-062282ea0dcf // indirect
github.com/pkg/errors v0.8.1 github.com/pkg/errors v0.8.1
github.com/spf13/jwalterweatherman v1.1.0 // indirect github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/viper v1.4.0 github.com/spf13/viper v1.5.0
github.com/stretchr/testify v1.4.0 github.com/stretchr/testify v1.4.0
github.com/ugorji/go v1.1.7 // indirect github.com/ugorji/go v1.1.7 // indirect
github.com/urfave/cli v1.22.1 github.com/urfave/cli v1.22.1

6
go.sum
View File

@@ -279,6 +279,8 @@ github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnIn
github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s= github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s=
github.com/spf13/viper v1.4.0 h1:yXHLWeravcrgGyFSyCgdYpXQ9dR9c/WED3pg1RhxqEU= github.com/spf13/viper v1.4.0 h1:yXHLWeravcrgGyFSyCgdYpXQ9dR9c/WED3pg1RhxqEU=
github.com/spf13/viper v1.4.0/go.mod h1:PTJ7Z/lr49W6bUbkmS1V3by4uWynFiR9p7+dSq/yZzE= github.com/spf13/viper v1.4.0/go.mod h1:PTJ7Z/lr49W6bUbkmS1V3by4uWynFiR9p7+dSq/yZzE=
github.com/spf13/viper v1.5.0 h1:GpsTwfsQ27oS/Aha/6d1oD7tpKIqWnOA6tgOX9HHkt4=
github.com/spf13/viper v1.5.0/go.mod h1:AkYRkVJF8TkSG/xet6PzXX+l39KhhXa2pdqVSxnTcn4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1 h1:2vfRuCMp5sSVIDSqO8oNnWJq7mPa6KVP3iPIwFBuy8A= github.com/stretchr/objx v0.1.1 h1:2vfRuCMp5sSVIDSqO8oNnWJq7mPa6KVP3iPIwFBuy8A=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
@@ -288,6 +290,8 @@ github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s=
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
github.com/tj/assert v0.0.0-20171129193455-018094318fb0/go.mod h1:mZ9/Rh9oLWpLLDRpvE+3b7gP/C2YyLFYxNmcLnPTMe0= github.com/tj/assert v0.0.0-20171129193455-018094318fb0/go.mod h1:mZ9/Rh9oLWpLLDRpvE+3b7gP/C2YyLFYxNmcLnPTMe0=
github.com/tj/go-elastic v0.0.0-20171221160941-36157cbbebc2/go.mod h1:WjeM0Oo1eNAjXGDx2yma7uG2XoyRZTq1uv3M/o7imD0= github.com/tj/go-elastic v0.0.0-20171221160941-36157cbbebc2/go.mod h1:WjeM0Oo1eNAjXGDx2yma7uG2XoyRZTq1uv3M/o7imD0=
github.com/tj/go-kinesis v0.0.0-20171128231115-08b17f58cb1b/go.mod h1:/yhzCV0xPfx6jb1bBgRFjl5lytqVqZXEaeqWP8lTEao= github.com/tj/go-kinesis v0.0.0-20171128231115-08b17f58cb1b/go.mod h1:/yhzCV0xPfx6jb1bBgRFjl5lytqVqZXEaeqWP8lTEao=
@@ -423,6 +427,8 @@ gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bl
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo= gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo=
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw= gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=

17
hack/install_docker.sh Executable file
View File

@@ -0,0 +1,17 @@
#!/bin/bash
set -e
# ++
# verified on Ubuntu 16.04 x64
# ++
user_host=$1
ssh ${user_host} 'bash -s' <<EOF
apt-get remove -y docker docker-engine docker.io containerd runc
apt-get update -y
apt-get install -y apt-transport-https ca-certificates curl software-properties-common lsb-core
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -
add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu \$(lsb_release -cs) stable"
apt-get update -y
apt-get install -y docker-ce
docker run hello-world
EOF

View File

@@ -5,25 +5,19 @@ import (
"strings" "strings"
"github.com/apex/log" "github.com/apex/log"
"github.com/metrue/fx/config" "github.com/metrue/fx/context"
"github.com/metrue/fx/constants"
api "github.com/metrue/fx/container_runtimes/docker/http"
"github.com/metrue/fx/packer" "github.com/metrue/fx/packer"
"github.com/metrue/fx/types" "github.com/metrue/fx/types"
"github.com/metrue/fx/utils" "github.com/metrue/fx/utils"
"github.com/urfave/cli"
) )
// Call command handle // Call command handle
func Call(cfg config.Configer) HandleFunc { func Call() HandleFunc {
return func(ctx *cli.Context) error { return func(ctx *context.Context) error {
params := strings.Join(ctx.Args()[1:], " ") cli := ctx.GetCliContext()
hosts, err := cfg.ListActiveMachines() _ = strings.Join(cli.Args()[1:], " ")
if err != nil {
log.Fatalf("list active machines failed: %v", err)
}
file := ctx.Args().First() file := cli.Args().First()
src, err := ioutil.ReadFile(file) src, err := ioutil.ReadFile(file)
if err != nil { if err != nil {
log.Fatalf("Read Source: %v", err) log.Fatalf("Read Source: %v", err)
@@ -36,17 +30,16 @@ func Call(cfg config.Configer) HandleFunc {
Language: lang, Language: lang,
Source: string(src), Source: string(src),
} }
project, err := packer.Pack(file, fn) if _, err := packer.Pack(file, fn); err != nil {
if err != nil {
panic(err) panic(err)
} }
for name, host := range hosts { // TODO not supported
if err := api.MustCreate(host.Host, constants.AgentPort). // if err := api.MustCreate(host.Host, constants.AgentPort).
Call(file, params, project); err != nil { // Call(file, params, project); err != nil {
log.Fatalf("call functions on machine %s with %v failed: %v", name, params, err) // log.Fatalf("call functions on machine %s with %v failed: %v", name, params, err)
} // }
}
return nil return nil
} }
} }

View File

@@ -1,102 +0,0 @@
package handlers
import (
"context"
"fmt"
"io/ioutil"
"os"
"github.com/apex/log"
"github.com/metrue/fx/config"
"github.com/metrue/fx/constants"
api "github.com/metrue/fx/container_runtimes/docker/http"
"github.com/metrue/fx/deploy"
dockerDeployer "github.com/metrue/fx/deploy/docker"
k8sDeployer "github.com/metrue/fx/deploy/kubernetes"
"github.com/metrue/fx/types"
"github.com/metrue/fx/utils"
"github.com/pkg/errors"
"github.com/urfave/cli"
)
// Deploy deploy handle function
func Deploy(cfg config.Configer) HandleFunc {
return func(ctx *cli.Context) (err error) {
funcFile := ctx.Args().First()
name := ctx.String("name")
port := ctx.Int("port")
force := ctx.Bool("force")
defer func() {
if r := recover(); r != nil {
log.Fatalf("fatal error happened: %v", r)
}
if err != nil {
log.Fatalf("deploy function %s (%s) failed: %v", name, funcFile, err)
}
log.Infof("function %s (%s) deployed successfully", name, funcFile)
}()
if port < PortRange.min || port > PortRange.max {
return fmt.Errorf("invalid port number: %d, port number should in range of %d - %d", port, PortRange.min, PortRange.max)
}
hosts, err := cfg.ListActiveMachines()
if err != nil {
return errors.Wrap(err, "list active machines failed")
}
if len(hosts) == 0 {
log.Warnf("no active machines")
return nil
}
// try to stop service firt
if force {
for n, host := range hosts {
if err := api.MustCreate(host.Host, constants.AgentPort).
Stop(name); err != nil {
log.Infof("stop function %s on machine %s failed: %v", name, n, err)
} else {
log.Infof("stop function %s on machine %s: %v", name, n, constants.CheckedSymbol)
}
}
}
body, err := ioutil.ReadFile(funcFile)
if err != nil {
return errors.Wrap(err, "read source failed")
}
lang := utils.GetLangFromFileName(funcFile)
var deployer deploy.Deployer
if os.Getenv("KUBECONFIG") != "" {
deployer, err = k8sDeployer.Create()
if err != nil {
return err
}
} else {
bctx := context.Background()
deployer, err = dockerDeployer.CreateClient(bctx)
if err != nil {
return err
}
}
// TODO multiple ports support
return deployer.Deploy(
context.Background(),
types.Func{Language: lang, Source: string(body)},
name,
[]types.PortBinding{
types.PortBinding{
ServiceBindingPort: 80,
ContainerExposePort: constants.FxContainerExposePort,
},
types.PortBinding{
ServiceBindingPort: 443,
ContainerExposePort: constants.FxContainerExposePort,
},
},
)
}
}

View File

@@ -1,38 +0,0 @@
package handlers
import (
"context"
"os"
"github.com/metrue/fx/config"
"github.com/metrue/fx/deploy"
dockerDeployer "github.com/metrue/fx/deploy/docker"
k8sDeployer "github.com/metrue/fx/deploy/kubernetes"
"github.com/urfave/cli"
)
// Destroy command handle
func Destroy(cfg config.Configer) HandleFunc {
return func(ctx *cli.Context) (err error) {
services := ctx.Args()
c := context.Background()
var runner deploy.Deployer
if os.Getenv("KUBECONFIG") != "" {
runner, err = k8sDeployer.Create()
if err != nil {
return err
}
} else {
runner, err = dockerDeployer.CreateClient(c)
if err != nil {
return err
}
}
for _, svc := range services {
if err := runner.Destroy(c, svc); err != nil {
return err
}
}
return nil
}
}

View File

@@ -1,27 +1,27 @@
package handlers package handlers
import ( import (
"os"
"github.com/apex/log" "github.com/apex/log"
"github.com/metrue/fx/config"
"github.com/metrue/fx/constants" "github.com/metrue/fx/constants"
"github.com/metrue/fx/context"
"github.com/metrue/fx/doctor" "github.com/metrue/fx/doctor"
"github.com/urfave/cli"
) )
// Doctor command handle // Doctor command handle
func Doctor(cfg config.Configer) HandleFunc { func Doctor() HandleFunc {
return func(ctx *cli.Context) error { return func(ctx *context.Context) error {
hosts, err := cfg.ListMachines() host := os.Getenv("DOCKER_REMOTE_HOST_ADDR")
if err != nil { user := os.Getenv("DOCKER_REMOTE_HOST_USER")
log.Fatalf("list machines failed %v", err) password := os.Getenv("DOCKER_REMOTE_HOST_PASSWORD")
return nil if host == "" {
host = "localhost"
} }
for name, h := range hosts { if err := doctor.New(host, user, password).Start(); err != nil {
if err := doctor.New(h).Start(); err != nil { log.Warnf("machine %s is in dirty state: %v", host, err)
log.Warnf("machine %s is in dirty state: %v", name, err)
} else { } else {
log.Infof("machine %s is in healthy state: %s", name, constants.CheckedSymbol) log.Infof("machine %s is in healthy state: %s", host, constants.CheckedSymbol)
}
} }
return nil return nil
} }

View File

@@ -1,28 +1,20 @@
package handlers package handlers
import ( import (
"github.com/apex/log" "github.com/metrue/fx/context"
"github.com/metrue/fx/config" "github.com/metrue/fx/deploy"
"github.com/metrue/fx/constants"
api "github.com/metrue/fx/container_runtimes/docker/http"
"github.com/pkg/errors"
"github.com/urfave/cli"
) )
// Down command handle // Down command handle
func Down(cfg config.Configer) HandleFunc { func Down() HandleFunc {
return func(ctx *cli.Context) error { return func(ctx *context.Context) (err error) {
containerID := ctx.Args() cli := ctx.GetCliContext()
hosts, err := cfg.ListActiveMachines() services := cli.Args()
if err != nil { runner := ctx.Get("deployer").(deploy.Deployer)
return errors.Wrapf(err, "list active machines failed: %v", err) for _, svc := range services {
if err := runner.Destroy(ctx.Context, svc); err != nil {
return err
} }
for name, host := range hosts {
if err := api.MustCreate(host.Host, constants.AgentPort).
Down(containerID); err != nil {
return errors.Wrapf(err, "stop function on machine %s failed: %v", name, err)
}
log.Infof("stop function on machine %s: %v", name, constants.CheckedSymbol)
} }
return nil return nil
} }

View File

@@ -1,6 +1,8 @@
package handlers package handlers
import "github.com/urfave/cli" import (
"github.com/metrue/fx/context"
)
// HandleFunc command handle function // HandleFunc command handle function
type HandleFunc func(ctx *cli.Context) error type HandleFunc func(ctx *context.Context) error

View File

@@ -1,56 +0,0 @@
package handlers
import (
"log"
"github.com/metrue/fx/config"
"github.com/metrue/fx/utils"
"github.com/urfave/cli"
)
// AddHost add a host
func AddHost(cfg config.Configer) HandleFunc {
return func(ctx *cli.Context) error {
name := ctx.String("name")
addr := ctx.String("host")
user := ctx.String("user")
password := ctx.String("password")
host := config.NewHost(addr, user, password)
if !host.Valid() {
log.Fatalf("invaid host %v", host)
return nil
}
if host.IsRemote() {
if host.User == "" || host.Password == "" {
log.Fatalf("the host to add is a remote, user and password for SSH login is required")
return nil
}
}
return cfg.AddMachine(name, host)
}
}
// RemoveHost remove a host
func RemoveHost(cfg config.Configer) HandleFunc {
return func(ctx *cli.Context) error {
name := ctx.Args().First()
if name == "" {
log.Fatalf("no name given: fx infra remove <name>")
return nil
}
return cfg.RemoveHost(name)
}
}
// ListHosts list hosts
func ListHosts(cfg config.Configer) HandleFunc {
return func(ctx *cli.Context) error {
hosts, err := cfg.ListMachines()
if err != nil {
return err
}
return utils.OutputJSON(hosts)
}
}

View File

@@ -4,29 +4,32 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os" "os"
"time"
"github.com/apex/log" "github.com/apex/log"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/metrue/fx/config"
"github.com/metrue/fx/constants" "github.com/metrue/fx/constants"
api "github.com/metrue/fx/container_runtimes/docker/http" containerruntimes "github.com/metrue/fx/container_runtimes"
"github.com/metrue/fx/context"
"github.com/metrue/fx/packer" "github.com/metrue/fx/packer"
"github.com/metrue/fx/provision"
"github.com/metrue/fx/types" "github.com/metrue/fx/types"
"github.com/metrue/fx/utils" "github.com/metrue/fx/utils"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/urfave/cli"
) )
// BuildImage build image // BuildImage build image
func BuildImage(cfg config.Configer) HandleFunc { func BuildImage() HandleFunc {
return func(ctx *cli.Context) error { return func(ctx *context.Context) error {
funcFile := ctx.Args().First() cli := ctx.GetCliContext()
tag := ctx.String("tag") funcFile := cli.Args().First()
tag := cli.String("tag")
if tag == "" { if tag == "" {
tag = uuid.New().String() tag = uuid.New().String()
} }
workdir := fmt.Sprintf("/tmp/fx-%d", time.Now().Unix())
defer os.RemoveAll(workdir)
body, err := ioutil.ReadFile(funcFile) body, err := ioutil.ReadFile(funcFile)
if err != nil { if err != nil {
log.Fatalf("function code load failed: %v", err) log.Fatalf("function code load failed: %v", err)
@@ -34,57 +37,33 @@ func BuildImage(cfg config.Configer) HandleFunc {
} }
log.Infof("function code loaded: %v", constants.CheckedSymbol) log.Infof("function code loaded: %v", constants.CheckedSymbol)
lang := utils.GetLangFromFileName(funcFile) lang := utils.GetLangFromFileName(funcFile)
pwd, err := os.Getwd()
if err != nil { fn := types.Func{Language: lang, Source: string(body)}
log.Fatalf("could not get current work directory: %v", err)
if err := packer.PackIntoDir(fn, workdir); err != nil {
log.Fatalf("could not pack function %v: %v", fn, err)
return err return err
} }
tarFile := fmt.Sprintf("%s.%s.tar", pwd, tag)
defer os.RemoveAll(tarFile)
if err := packer.PackIntoTar(types.Func{Language: lang, Source: string(body)}, tarFile); err != nil { docker, ok := ctx.Get("docker").(containerruntimes.ContainerRuntime)
log.Fatalf("could not pack function: %v", err) if ok {
nameWithTag := tag + ":latest"
if err := docker.BuildImage(ctx.Context, workdir, nameWithTag); err != nil {
return err return err
} }
log.Infof("function packed: %v", constants.CheckedSymbol) log.Infof("image built: %v", constants.CheckedSymbol)
hosts, err := cfg.ListActiveMachines()
if err != nil {
log.Fatalf("could not list active machine: %v", err)
return errors.Wrap(err, "list active machines failed")
}
if len(hosts) == 0 {
log.Warnf("no active machines")
return nil return nil
} }
for n, host := range hosts { return fmt.Errorf("no available docker cli")
if !host.Provisioned {
provisionor := provision.New(host)
if err := provisionor.Start(); err != nil {
return errors.Wrapf(err, "could not provision %s", n)
}
log.Infof("provision machine %v: %s", n, constants.CheckedSymbol)
if err := cfg.UpdateProvisionedStatus(n, true); err != nil {
return errors.Wrap(err, "update machine provision status failed")
}
}
if err := api.MustCreate(host.Host, constants.AgentPort).
BuildImage(tarFile, tag, map[string]string{}); err != nil {
return err
}
log.Infof("image built on machine %s: %v", n, constants.CheckedSymbol)
}
return nil
} }
} }
// ExportImage export service's code into a directory // ExportImage export service's code into a directory
func ExportImage() HandleFunc { func ExportImage() HandleFunc {
return func(ctx *cli.Context) (err error) { return func(ctx *context.Context) (err error) {
funcFile := ctx.Args().First() cli := ctx.GetCliContext()
outputDir := ctx.String("output") funcFile := cli.Args().First()
outputDir := cli.String("output")
if outputDir == "" { if outputDir == "" {
log.Fatalf("output directory required") log.Fatalf("output directory required")
return nil return nil

View File

@@ -1,46 +0,0 @@
package handlers
import (
"github.com/apex/log"
"github.com/metrue/fx/config"
"github.com/metrue/fx/constants"
"github.com/metrue/fx/provision"
"github.com/urfave/cli"
)
// Activate a machine to be fx server
func Activate(cfg config.Configer) HandleFunc {
return func(ctx *cli.Context) error {
name := ctx.Args().First()
if name == "" {
log.Fatalf("name required for: fx infra activate <name>")
return nil
}
host, err := cfg.GetMachine(name)
if err != nil {
log.Fatalf("could get host %v, make sure you add it first", err)
log.Info("You can add a machine by: \n fx infra add -Name <name> -H <ip or hostname> -U <user> -P <password>")
return nil
}
if !host.Provisioned {
provisionor := provision.New(host)
if err := provisionor.Start(); err != nil {
log.Fatalf("could not provision %s: %v", name, err)
return nil
}
log.Infof("provision machine %v: %s", name, constants.CheckedSymbol)
if err := cfg.UpdateProvisionedStatus(name, true); err != nil {
log.Fatalf("update machine provision status failed: %v", err)
}
}
if err := cfg.EnableMachine(name); err != nil {
log.Fatalf("could not enable %s: %v", name, err)
return nil
}
log.Infof("enble machine %v: %s", name, constants.CheckedSymbol)
return nil
}
}

View File

@@ -1,25 +0,0 @@
package handlers
import (
"github.com/apex/log"
"github.com/metrue/fx/config"
"github.com/metrue/fx/constants"
"github.com/urfave/cli"
)
// Deactivate a machine
func Deactivate(cfg config.Configer) HandleFunc {
return func(ctx *cli.Context) error {
name := ctx.Args().First()
if name == "" {
log.Fatalf("name required for: fx infra activate <name>")
return nil
}
if err := cfg.DisableMachine(name); err != nil {
log.Fatalf("could not disable %s: %v", name, err)
return nil
}
log.Infof("machine %s deactive: %v", name, constants.CheckedSymbol)
return nil
}
}

31
handlers/init.go Normal file
View File

@@ -0,0 +1,31 @@
package handlers
import (
"os"
"github.com/apex/log"
"github.com/metrue/fx/context"
"github.com/metrue/fx/provision"
)
// Init start fx-agent
func Init() HandleFunc {
return func(ctx *context.Context) error {
host := os.Getenv("DOCKER_REMOTE_HOST_ADDR")
user := os.Getenv("DOCKER_REMOTE_HOST_USER")
passord := os.Getenv("DOCKER_REMOTE_HOST_PASSWORD")
if host == "" {
host = "127.0.0.1"
}
provisioner := provision.NewWithHost(host, user, passord)
if !provisioner.IsFxAgentRunning() {
if err := provisioner.StartFxAgent(); err != nil {
log.Fatalf("could not start fx agent on host: %s", err)
return err
}
log.Info("fx agent started")
}
log.Info("fx agent already started")
return nil
}
}

View File

@@ -1,25 +1,28 @@
package handlers package handlers
import ( import (
"github.com/apex/log" "github.com/metrue/fx/context"
"github.com/metrue/fx/config" "github.com/metrue/fx/deploy"
"github.com/metrue/fx/constants" "github.com/metrue/fx/utils"
api "github.com/metrue/fx/container_runtimes/docker/http"
"github.com/urfave/cli"
) )
// List command handle // List command handle
func List(cfg config.Configer) HandleFunc { func List() HandleFunc {
return func(ctx *cli.Context) error { return func(ctx *context.Context) error {
hosts, err := cfg.ListActiveMachines() cli := ctx.GetCliContext()
deployer := ctx.Get("deployer").(deploy.Deployer)
services, err := deployer.List(ctx.Context, cli.Args().First())
if err != nil { if err != nil {
log.Fatalf("list active machines failed: %v", err) return err
} }
for name, host := range hosts {
if err := api.MustCreate(host.Host, constants.AgentPort).List(ctx.Args().First()); err != nil { for _, service := range services {
log.Fatalf("list functions on machine %s failed: %v", name, err) if err := utils.OutputJSON(service); err != nil {
return err
} }
} }
return nil return nil
} }
} }

View File

@@ -5,15 +5,11 @@ import (
"io/ioutil" "io/ioutil"
"github.com/apex/log" "github.com/apex/log"
"github.com/metrue/fx/config" "github.com/metrue/fx/context"
"github.com/metrue/fx/constants" "github.com/metrue/fx/deploy"
api "github.com/metrue/fx/container_runtimes/docker/http"
"github.com/metrue/fx/packer"
"github.com/metrue/fx/provision"
"github.com/metrue/fx/types" "github.com/metrue/fx/types"
"github.com/metrue/fx/utils" "github.com/metrue/fx/utils"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/urfave/cli"
) )
// PortRange usable port range https: //en.wikipedia.org/wiki/Ephemeral_port // PortRange usable port range https: //en.wikipedia.org/wiki/Ephemeral_port
@@ -26,13 +22,12 @@ var PortRange = struct {
} }
// Up command handle // Up command handle
func Up(cfg config.Configer) HandleFunc { func Up() HandleFunc {
return func(ctx *cli.Context) (err error) { return func(ctx *context.Context) (err error) {
funcFile := ctx.Args().First() cli := ctx.GetCliContext()
name := ctx.String("name") funcFile := cli.Args().First()
port := ctx.Int("port") name := cli.String("name")
healtcheck := ctx.Bool("healthcheck") port := cli.Int("port")
force := ctx.Bool("force")
defer func() { defer func() {
if r := recover(); r != nil { if r := recover(); r != nil {
@@ -48,69 +43,19 @@ func Up(cfg config.Configer) HandleFunc {
if port < PortRange.min || port > PortRange.max { if port < PortRange.min || port > PortRange.max {
return fmt.Errorf("invalid port number: %d, port number should in range of %d - %d", port, PortRange.min, PortRange.max) return fmt.Errorf("invalid port number: %d, port number should in range of %d - %d", port, PortRange.min, PortRange.max)
} }
hosts, err := cfg.ListActiveMachines()
if err != nil {
return errors.Wrap(err, "list active machines failed")
}
if len(hosts) == 0 {
log.Warnf("no active machines")
return nil
}
// try to stop service firt
if force {
for n, host := range hosts {
if err := api.MustCreate(host.Host, constants.AgentPort).
Stop(name); err != nil {
log.Infof("stop function %s on machine %s failed: %v", name, n, err)
} else {
log.Infof("stop function %s on machine %s: %v", name, n, constants.CheckedSymbol)
}
}
}
body, err := ioutil.ReadFile(funcFile) body, err := ioutil.ReadFile(funcFile)
if err != nil { if err != nil {
return errors.Wrap(err, "read source failed") return errors.Wrap(err, "read source failed")
} }
lang := utils.GetLangFromFileName(funcFile) lang := utils.GetLangFromFileName(funcFile)
deployer := ctx.Get("deployer").(deploy.Deployer)
fn := types.Func{ bindings := ctx.Get("bindings").([]types.PortBinding)
Language: lang, return deployer.Deploy(
Source: string(body), ctx.Context,
} types.Func{Language: lang, Source: string(body)},
name,
project, err := packer.Pack(name, fn) bindings,
if err != nil { )
return errors.Wrapf(err, "could pack function %s (%s)", name, funcFile)
}
for n, host := range hosts {
if !host.Provisioned {
provisionor := provision.New(host)
if err := provisionor.Start(); err != nil {
return errors.Wrapf(err, "could not provision %s", n)
}
log.Infof("provision machine %v: %s", n, constants.CheckedSymbol)
if err := cfg.UpdateProvisionedStatus(n, true); err != nil {
return errors.Wrap(err, "update machine provision status failed")
}
}
if err := api.MustCreate(host.Host, constants.AgentPort).
Up(api.UpOptions{
Body: body,
Lang: lang,
Name: name,
Port: port,
HealtCheck: healtcheck,
Project: project,
}); err != nil {
return errors.Wrapf(err, "up function %s(%s) to machine %s failed", name, funcFile, n)
}
log.Infof("up function %s(%s) to machine %s: %v", name, funcFile, n, constants.CheckedSymbol)
}
return nil
} }
} }

38
middlewares/binding.go Normal file
View File

@@ -0,0 +1,38 @@
package middlewares
import (
"os"
"github.com/metrue/fx/constants"
"github.com/metrue/fx/context"
"github.com/metrue/fx/types"
)
// Binding create bindings
func Binding(ctx *context.Context) error {
cli := ctx.GetCliContext()
port := cli.Int("port")
var bindings []types.PortBinding
if os.Getenv("KUBECONFIG") != "" {
bindings = []types.PortBinding{
types.PortBinding{
ServiceBindingPort: 80,
ContainerExposePort: constants.FxContainerExposePort,
},
types.PortBinding{
ServiceBindingPort: 443,
ContainerExposePort: constants.FxContainerExposePort,
},
}
} else {
bindings = []types.PortBinding{
types.PortBinding{
ServiceBindingPort: int32(port),
ContainerExposePort: constants.FxContainerExposePort,
},
}
}
ctx.Set("bindings", bindings)
return nil
}

49
middlewares/setup.go Normal file
View File

@@ -0,0 +1,49 @@
package middlewares
import (
"os"
"github.com/metrue/fx/constants"
containerruntimes "github.com/metrue/fx/container_runtimes"
dockerHTTP "github.com/metrue/fx/container_runtimes/docker/http"
dockerSDK "github.com/metrue/fx/container_runtimes/docker/sdk"
"github.com/metrue/fx/context"
"github.com/metrue/fx/deploy"
dockerDeployer "github.com/metrue/fx/deploy/docker"
k8sDeployer "github.com/metrue/fx/deploy/kubernetes"
)
// Setup create k8s or docker cli
func Setup(ctx *context.Context) (err error) {
var deployer deploy.Deployer
if os.Getenv("KUBECONFIG") != "" {
deployer, err = k8sDeployer.Create()
if err != nil {
return err
}
} else {
deployer, err = dockerDeployer.CreateClient(ctx.Context)
if err != nil {
return err
}
}
ctx.Set("deployer", deployer)
host := os.Getenv("DOCKER_REMOTE_HOST_ADDR")
user := os.Getenv("DOCKER_REMOTE_HOST_USER")
var docker containerruntimes.ContainerRuntime
if host != "" && user != "" {
docker, err = dockerHTTP.Create(host, constants.AgentPort)
if err != nil {
return err
}
} else {
docker, err = dockerSDK.CreateClient(ctx)
if err != nil {
return err
}
}
ctx.Set("docker", docker)
return nil
}

File diff suppressed because one or more lines are too long

View File

@@ -20,7 +20,7 @@ module.exports = ({a, b}) => {
return a + b return a + b
} }
` `
fn := types.ServiceFunctionSource{ fn := types.Func{
Language: "node", Language: "node",
Source: mockSource, Source: mockSource,
} }

View File

@@ -1,8 +1,11 @@
FROM metrue/fx-go-base:latest FROM golang:latest
COPY . /go/src/github.com/metrue/fx COPY . /go/src/github.com/metrue/fx
WORKDIR /go/src/github.com/metrue/fx WORKDIR /go/src/github.com/metrue/fx
# dependency management
RUN go get github.com/gin-gonic/gin
RUN go build -ldflags "-w -s" -o fx fx.go app.go RUN go build -ldflags "-w -s" -o fx fx.go app.go
EXPOSE 3000 EXPOSE 3000

View File

@@ -1,7 +1,6 @@
package packer package packer
import ( import (
"encoding/base64"
"testing" "testing"
"github.com/metrue/fx/types" "github.com/metrue/fx/types"
@@ -13,7 +12,7 @@ module.exports = ({a, b}) => {
return a + b return a + b
} }
` `
fn := types.ServiceFunctionSource{ fn := types.Func{
Language: "node", Language: "node",
Source: mockSource, Source: mockSource,
} }
@@ -70,16 +69,12 @@ public class Fx {
} }
} }
` `
fn := types.ServiceFunctionSource{ fn := types.Func{
Language: "java", Language: "java",
Source: mockSource, Source: mockSource,
} }
tree, err := PackIntoK8SConfigMapFile(fn.Language, fn.Source) _, err := PackIntoK8SConfigMapFile(fn)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
body := base64.StdEncoding.EncodeToString([]byte(mockSource))
if tree["src/main/java/fx/Fx.java"] != body {
t.Fatalf("should get %s but got %s", body, tree["src/main/java/fx/app.java"])
}
} }

View File

@@ -2,11 +2,10 @@ package provision
import ( import (
"fmt" "fmt"
"strings" "os"
"sync" "sync"
"github.com/apex/log" "github.com/apex/log"
"github.com/metrue/fx/config"
"github.com/metrue/fx/constants" "github.com/metrue/fx/constants"
"github.com/metrue/fx/pkg/command" "github.com/metrue/fx/pkg/command"
ssh "github.com/metrue/go-ssh-client" ssh "github.com/metrue/go-ssh-client"
@@ -20,25 +19,82 @@ type Provisioner interface {
// Provisionor provision-or // Provisionor provision-or
type Provisionor struct { type Provisionor struct {
sshClient ssh.Client sshClient ssh.Client
host string
host config.Host
} }
// New new provision func isLocal(host string) bool {
func New(host config.Host) *Provisionor { if host == "" {
p := &Provisionor{host: host} return false
if host.IsRemote() { }
p.sshClient = ssh.New(host.Host). return host == "127.0.0.1" || host == "localhost" || host == "0.0.0.0"
WithUser(host.User). }
WithPassword(host.Password)
// NewWithHost create a provisionor with host, user, and password
func NewWithHost(host string, user string, password string) *Provisionor {
p := &Provisionor{
host: host,
}
if !isLocal(host) {
p.sshClient = ssh.New(host).
WithUser(user).
WithPassword(password)
} }
return p return p
} }
// IsFxAgentRunning check if fx-agent is running on host
func (p *Provisionor) IsFxAgentRunning() bool {
script := fmt.Sprintf("docker inspect %s", constants.AgentContainerName)
var cmd *command.Command
if !isLocal(p.host) {
cmd = command.New("inspect fx-agent", script, command.NewRemoteRunner(p.sshClient))
} else {
cmd = command.New("inspect fx-agent", script, command.NewLocalRunner())
}
output, err := cmd.Exec()
if os.Getenv("DEBUG") != "" {
log.Infof(string(output))
}
if err != nil {
return false
}
return true
}
// StartFxAgent start fx agent
func (p *Provisionor) StartFxAgent() error {
script := fmt.Sprintf("docker run -d --name=%s --rm -v /var/run/docker.sock:/var/run/docker.sock -p 0.0.0.0:%s:1234 bobrik/socat TCP-LISTEN:1234,fork UNIX-CONNECT:/var/run/docker.sock", constants.AgentContainerName, constants.AgentPort)
var cmd *command.Command
if !isLocal(p.host) {
cmd = command.New("start fx-agent", script, command.NewRemoteRunner(p.sshClient))
} else {
cmd = command.New("start fx-agent", script, command.NewLocalRunner())
}
if output, err := cmd.Exec(); err != nil {
log.Info(string(output))
return err
}
return nil
}
// StopFxAgent stop fx agent
func (p *Provisionor) StopFxAgent() error {
script := fmt.Sprintf("docker stop %s", constants.AgentContainerName)
var cmd *command.Command
if !isLocal(p.host) {
cmd = command.New("stop fx agent", script, command.NewRemoteRunner(p.sshClient))
} else {
cmd = command.New("stop fx agent", script, command.NewLocalRunner())
}
if output, err := cmd.Exec(); err != nil {
log.Infof(string(output))
return err
}
return nil
}
// Start start provision progress // Start start provision progress
func (p *Provisionor) Start() error { func (p *Provisionor) Start() error {
startFxAgent := fmt.Sprintf("docker run -d --name=%s --rm -v /var/run/docker.sock:/var/run/docker.sock -p 0.0.0.0:%s:1234 bobrik/socat TCP-LISTEN:1234,fork UNIX-CONNECT:/var/run/docker.sock", constants.AgentContainerName, constants.AgentPort)
stopFxAgent := fmt.Sprintf("docker stop %s", constants.AgentContainerName)
scripts := map[string]string{ scripts := map[string]string{
"pull java Docker base image": "docker pull metrue/fx-java-base", "pull java Docker base image": "docker pull metrue/fx-java-base",
"pull julia Docker base image": "docker pull metrue/fx-julia-base", "pull julia Docker base image": "docker pull metrue/fx-julia-base",
@@ -48,35 +104,12 @@ func (p *Provisionor) Start() error {
"pull go Docker base image": "docker pull metrue/fx-go-base", "pull go Docker base image": "docker pull metrue/fx-go-base",
} }
agentStartupCmds := []*command.Command{}
if p.host.IsRemote() {
agentStartupCmds = append(agentStartupCmds,
command.New("stop current fx agent", stopFxAgent, command.NewRemoteRunner(p.sshClient)),
command.New("start fx agent", startFxAgent, command.NewRemoteRunner(p.sshClient)),
)
} else {
agentStartupCmds = append(agentStartupCmds,
command.New("stop current fx agent", stopFxAgent, command.NewLocalRunner()),
command.New("start fx agent", startFxAgent, command.NewLocalRunner()),
)
}
for _, cmd := range agentStartupCmds {
if output, err := cmd.Exec(); err != nil {
if strings.Contains(string(output), "No such container: fx-agent") {
// Skip stop a fx-agent error when there is not agent running
} else {
log.Fatalf("Provision:%s: %s, %s", cmd.Name, err, output)
return err
}
}
}
var wg sync.WaitGroup var wg sync.WaitGroup
for n, s := range scripts { for n, s := range scripts {
wg.Add(1) wg.Add(1)
go func(name, script string) { go func(name, script string) {
var cmd *command.Command var cmd *command.Command
if p.host.IsRemote() { if !isLocal(p.host) {
cmd = command.New(name, script, command.NewRemoteRunner(p.sshClient)) cmd = command.New(name, script, command.NewRemoteRunner(p.sshClient))
} else { } else {
cmd = command.New(name, script, command.NewLocalRunner()) cmd = command.New(name, script, command.NewLocalRunner())

View File

@@ -2,14 +2,35 @@ package provision
import ( import (
"testing" "testing"
"time"
"github.com/metrue/fx/config"
) )
func TestStart(t *testing.T) { func TestProvisionWorkflow(t *testing.T) {
host := config.Host{Host: "127.0.0.1"} provisionor := NewWithHost("127.0.0.1", "", "")
provisionor := New(host)
_ = provisionor.StopFxAgent()
// TODO wait too long here to make test pass
time.Sleep(40 * time.Second)
running := provisionor.IsFxAgentRunning()
if running {
t.Fatalf("fx-agent should not be running")
}
if err := provisionor.StartFxAgent(); err != nil {
t.Fatal(err)
}
running = provisionor.IsFxAgentRunning()
if !running {
t.Fatalf("fx-agent should be running")
}
if err := provisionor.Start(); err != nil { if err := provisionor.Start(); err != nil {
t.Fatal(err) t.Fatal(err)
} }
if err := provisionor.StopFxAgent(); err != nil {
t.Fatal(err)
}
} }

View File

@@ -10,12 +10,3 @@ sudo apt-get update -y
sudo apt-get install -y docker-ce sudo apt-get install -y docker-ce
docker run hello-world docker run hello-world
# curl -Lo kubectl https://storage.googleapis.com/kubernetes-release/release/${K8S_VERSION}/bin/linux/amd64/kubectl && chmod +x kubectl && sudo mv kubectl /usr/local/bin/
# mkdir -p ${HOME}/.kube
# touch ${HOME}/.kube/confi
#
## start fx proxy agent
docker run -d --name=fx-agent --rm -v /var/run/docker.sock:/var/run/docker.sock -p 0.0.0.0:8866:1234 bobrik/socat TCP-LISTEN:1234,fork UNIX-CONNECT:/var/run/docker.sock

View File

@@ -3,26 +3,15 @@
set -e set -e
fx="./build/fx" fx="./build/fx"
service='fx-service-abc' service='fx-service'
run() { run() {
local lang=$1 local lang=$1
local port=$2 local port=$2
# localhost
$fx up --name ${service}_${lang} --port ${port} --healthcheck test/functions/func.${lang} $fx up --name ${service}_${lang} --port ${port} --healthcheck test/functions/func.${lang}
$fx list # | jq '' $fx list # | jq ''
$fx down ${service}_${lang} # | grep "Down Service ${service}" $fx down ${service}_${lang} || true
}
deploy() {
local lang=$1
local port=$2
if [[ -z "$DOCKER_USERNAME" || -z "$DOCKER_PASSWORD" ]];then
echo "skip deploy test since no DOCKER_USERNAME and DOCKER_PASSWORD set"
else
$fx deploy --name ${service}-${lang} --port ${port} test/functions/func.${lang}
docker ps
$fx destroy ${service}-${lang}
fi
} }
build_image() { build_image() {
@@ -39,16 +28,13 @@ export_image() {
# main # main
# clean up # clean up
docker stop fx-agent || true && docker rm fx-agent || true # docker stop fx-agent || true && docker rm fx-agent || true
$fx infra activate localhost
port=20000 port=20000
for lang in 'js' 'rb' 'py' 'go' 'php' 'java' 'd'; do for lang in 'js' 'rb' 'py' 'go' 'php' 'java' 'd'; do
run $lang $port run $lang $port
((port++)) ((port++))
deploy $lang $port
build_image $lang "test-fx-image-build-${lang}" build_image $lang "test-fx-image-build-${lang}"
mkdir -p /tmp/${lang}/images mkdir -p /tmp/${lang}/images
export_image ${lang} /tmp/${lang}/images export_image ${lang} /tmp/${lang}/images

View File

@@ -1,15 +0,0 @@
package utils
import "testing"
func TestDockerVersion(t *testing.T) {
host := "localhost"
port := "8866"
version, err := DockerVersion(host, port)
if err != nil {
t.Fatal(err)
}
if version == "" {
t.Fatal("should version empty")
}
}