Skip to content

Commit 1ba7f35

Browse files
authored
Add in cli to publish (#144)
* Add in cli to publish Signed-off-by: Matthew B White <whitemat@uk.ibm.com> * Docs updates Signed-off-by: Matthew B White <whitemat@uk.ibm.com> --------- Signed-off-by: Matthew B White <whitemat@uk.ibm.com>
1 parent c85c234 commit 1ba7f35

File tree

9 files changed

+198
-42
lines changed

9 files changed

+198
-42
lines changed

.github/workflows/publish.yml

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
# Build the daemon binary and if a release publish if a 'tag' build
1616
# amd64/arm64
1717
binary_build:
18-
name: Binary Build
18+
name: Binary Daemon Build
1919
runs-on: ubuntu-latest
2020
strategy:
2121
matrix:
@@ -25,10 +25,10 @@ jobs:
2525
steps:
2626
- name: Checkout
2727
uses: actions/checkout@v2
28-
- name: Use Go 1.17
28+
- name: Use Go 1.18
2929
uses: actions/setup-go@v2
3030
with:
31-
go-version: 1.17
31+
go-version: 1.18
3232
- name: Build Binary
3333
run: go build -v -o bin/microfabd cmd/microfabd/main.go
3434
- name: Package Binary
@@ -41,6 +41,31 @@ jobs:
4141
with:
4242
files: microfab-*.tgz
4343

44+
# Build the cli binary and if a release publish if a 'tag' build
45+
# amd64/arm64
46+
binary_cli_build:
47+
name: Binary CLI Build
48+
runs-on: ubuntu-latest
49+
strategy:
50+
matrix:
51+
goarch: [amd64, arm64]
52+
env:
53+
GOARCH: ${{ matrix.goarch }}
54+
steps:
55+
- name: Checkout
56+
uses: actions/checkout@v2
57+
- name: Use Go 1.18
58+
uses: actions/setup-go@v2
59+
with:
60+
go-version: 1.18
61+
- name: Build Binary
62+
run: go build -v -o bin/microfab-${GOARCH} cmd/microfab/main.go
63+
- name: Publish Binary to GitHub Release
64+
uses: softprops/action-gh-release@v1
65+
if: startsWith(github.ref, 'refs/tags/')
66+
with:
67+
files: bin/microfab-${GOARCH}
68+
4469
# Build the container images and push to the ghcr.io repo
4570
# amd64/arm64
4671
container_build:

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ Check the [reference](./docs/DevelopingContracts.md) in this repo for details in
1212
## Tutorial
1313

1414
Check the [Quick Start Tutorial](./docs/Tutorial.md) - nothing to deployed smart contract in under 5minutes;
15+
16+
```
17+
curl -sSL https://github.com/hyperledger-labs/microfab/releases/download/v0.0.18/microfab-linux-amd64 -o microfab
18+
microfab start --log
19+
```
20+
1521
## Why microfab?
1622

1723
There are other 'form factors' of Fabric some are aimed at production/k8s deployments others more development focussed.

docs/Tutorial.md

Lines changed: 74 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,21 @@ curl -sSL https://github.com/hyperledger-labs/microfab/raw/main/integration/data
2222

2323
- Start Microfab with it's default configuration; (in a separate terminal run `docker logs -f microfab` so you can see what it's doing)
2424
```
25-
docker run -d --rm -p 8080:8080 --name microfab ghcr.io/hyperledger-labs/microfab:latest
25+
curl -sSL https://github.com/hyperledger-labs/microfab/releases/download/v0.0.18/microfab-linux-amd64 -o microfab
26+
27+
microfab start
2628
```
2729

2830
- We need to get the configuration of microfab and the address identities that it created; using the Hyperledger Labs *weft* tool is the quickest
31+
2932
```
30-
curl -s http://console.127-0-0-1.nip.io:8080/ak/api/v1/components | npx @hyperledger-labs/weft microfab -w _wallets -p _gateways -m _msp -f
33+
microfab connect
3134
```
3235

33-
- This will show us some environment variables we can use to work with Fabric.
36+
- This writes out a certificates and keys in a structure to use with the PeerCLI. Set the current shell enviroment variables for org1
3437

3538
```
36-
export CORE_PEER_LOCALMSPID=Org1MSP
37-
export CORE_PEER_MSPCONFIGPATH=$(pwd)/_msp/Org1/org1admin/msp
38-
export CORE_PEER_ADDRESS=org1peer-api.127-0-0-1.nip.io:8080
39+
source _mfcfg/org1.env
3940
```
4041

4142
- We can then Install, Approve and Commit the chaincode definition
@@ -78,4 +79,71 @@ peer chaincode invoke -C channel1 -n assettx \
7879
-c '{"Args":["org.hyperledger.fabric:GetMetadata"]}' \
7980
--orderer orderer-api.127-0-0-1.nip.io:8080 2>&1 \
8081
git stat| sed -e 's/^.*payload://' | sed -e 's/..$//' -e 's/^.//' -e 's/\\"/"/g' | jq
82+
```
83+
84+
## Microfab CLI
85+
86+
The CLI is a small binary wrapper that will create the docker image (pulling the image if needed), and write out the identitiy information.
87+
88+
The (original) way was to run the docker commands manually, see below for the equivalents
89+
90+
```
91+
Microfab Launch Control
92+
93+
Usage:
94+
microfab [command]
95+
96+
microfab
97+
connect Writes out connection details for use by the Peer CLI and SDKs
98+
ping Pings the microfab image to see if it's running
99+
start Starts the microfab image running
100+
stop Stops the microfab image running
101+
102+
Additional Commands:
103+
completion Generate the autocompletion script for the specified shell
104+
help Help about any command
105+
106+
Flags:
107+
-h, --help help for microfab
108+
-v, --version version for microfab
109+
110+
```
111+
112+
### Start
113+
```
114+
Starts the microfab image running
115+
116+
Usage:
117+
microfab start [flags]
118+
119+
Flags:
120+
--config string Microfab config (default "{\"endorsing_organizations\":[{\"name\":\"org1\"}],\"channels\":[{\"name\":\"mychannel\",\"endorsing_organizations\":[\"org1\"]},{\"name\":\"appchannel\",\"endorsing_organizations\":[\"org1\"]}],\"capability_level\":\"V2_5\"}")
121+
--configFile string Microfab config file
122+
-f, --force Force restart if microfab already running
123+
-h, --help help for start
124+
-l, --logs Display the logs (docker logs -f microfab)
125+
```
126+
127+
### Connect
128+
129+
```
130+
Writes out connection details for use by the Peer CLI and SDKs
131+
132+
Usage:
133+
microfab connect [flags]
134+
135+
Flags:
136+
-f, --force Force overwriting details directory
137+
-h, --help help for connect
138+
--msp string msp output directory (default "_mfcfg")
139+
```
140+
141+
## Docker Command Equivalents
142+
143+
```
144+
docker run -d --rm -p 8080:8080 --name microfab ghcr.io/hyperledger-labs/microfab:latest
145+
```
146+
147+
```
148+
curl -s http://console.127-0-0-1.nip.io:8080/ak/api/v1/components | npx @hyperledger-labs/weft microfab -w _wallets -p _gateways -m _msp -f
81149
```

pkg/microfab/connect.go

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,19 @@ import (
1414
)
1515

1616
var connectCmd = &cobra.Command{
17-
Use: "connect",
18-
Short: "Writes out connection details for use by the Peer CLI and SDKs",
17+
Use: "connect",
18+
Short: "Writes out connection details for use by the Peer CLI and SDKs",
19+
GroupID: "mf",
1920
RunE: func(cmd *cobra.Command, args []string) error {
2021
return connect()
2122
},
2223
}
2324

25+
func init() {
26+
connectCmd.PersistentFlags().BoolVarP(&force, "force", "f", false, "Force overwriting details directory")
27+
connectCmd.PersistentFlags().StringVar(&mspdir, "msp", "_mfcfg", "msp output directory")
28+
}
29+
2430
func connect() error {
2531

2632
urlStr := "http://console.127-0-0-1.nip.io:8080"
@@ -35,7 +41,7 @@ func connect() error {
3541
log.Printf("Identity and Configuration '%s'\n", rootDir)
3642

3743
// check to see if the directory exists, and if it does emptry
38-
cfgExists, err := exists(rootDir)
44+
cfgExists, err := Exists(rootDir)
3945
if err != nil {
4046
return err
4147
}
@@ -97,11 +103,13 @@ func connect() error {
97103
return errors.Wrapf(err, "Unable to form path for context")
98104
}
99105

100-
f.WriteString(fmt.Sprintf("CORE_PEER_ADDRESS=%s\n", u.Host))
101-
f.WriteString(fmt.Sprintf("CORE_PEER_LOCALMSPID=%s\n", peer.MSPID))
102-
f.WriteString(fmt.Sprintf("CORE_PEER_MSPCONFIGPATH=%s\n", idRoot))
106+
f.WriteString(fmt.Sprintf("export CORE_PEER_ADDRESS=%s\n", u.Host))
107+
f.WriteString(fmt.Sprintf("export CORE_PEER_LOCALMSPID=%s\n", peer.MSPID))
108+
f.WriteString(fmt.Sprintf("export CORE_PEER_MSPCONFIGPATH=%s\n", idRoot))
103109
f.Sync()
104110

111+
log.Printf("For %s context run 'source %s'", org, f.Name())
112+
105113
}
106114
return nil
107115

@@ -120,15 +128,3 @@ func isEmpty(name string) (bool, error) {
120128
}
121129
return false, err // Either not empty or error, suits both cases
122130
}
123-
124-
// exists returns whether the given file or directory exists
125-
func exists(path string) (bool, error) {
126-
_, err := os.Stat(path)
127-
if err == nil {
128-
return true, nil
129-
}
130-
if os.IsNotExist(err) {
131-
return false, nil
132-
}
133-
return false, err
134-
}

pkg/microfab/ping.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ import (
1010
)
1111

1212
var pingCmd = &cobra.Command{
13-
Use: "ping",
14-
Short: "Pings the microfab image to see if it's running",
13+
Use: "ping",
14+
Short: "Pings the microfab image to see if it's running",
15+
GroupID: "mf",
1516
RunE: func(cmd *cobra.Command, args []string) error {
1617
return ping()
1718
},

pkg/microfab/root.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@ var rootCmd = &cobra.Command{
1616
SilenceErrors: true,
1717
}
1818

19+
var defaultCfg = `{"endorsing_organizations":[{"name":"org1"}],"channels":[{"name":"mychannel","endorsing_organizations":["org1"]},{"name":"appchannel","endorsing_organizations":["org1"]}],"capability_level":"V2_5"}`
20+
1921
var cfg string
2022
var mspdir string
2123
var force bool
24+
var cfgFile string
2225

2326
// Execute the microfab command
2427
func Execute() {
@@ -31,12 +34,8 @@ func Execute() {
3134
}
3235

3336
func init() {
34-
rootCmd.PersistentFlags().StringVar(&cfg, "config", "", "Microfab config")
35-
rootCmd.PersistentFlags().StringVar(&mspdir, "msp", "_mfcfg", "msp output directory")
36-
rootCmd.PersistentFlags().BoolVar(&force, "force", false, "Force overwriting msp directory")
37-
38-
viper.BindPFlag("MICROFAB_CONFIG", rootCmd.PersistentFlags().Lookup("config"))
3937

38+
rootCmd.AddGroup(&cobra.Group{ID: "mf", Title: "microfab"})
4039
rootCmd.AddCommand(startCmd)
4140
rootCmd.AddCommand(stopCmd)
4241
rootCmd.AddCommand(connectCmd)

pkg/microfab/start.go

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,39 @@ import (
44
"context"
55
"fmt"
66
"log"
7+
"os"
78

89
"github.com/docker/docker/api/types"
910
"github.com/docker/docker/api/types/container"
1011
"github.com/docker/docker/client"
12+
"github.com/docker/docker/pkg/stdcopy"
1113
"github.com/docker/go-connections/nat"
1214
"github.com/pkg/errors"
1315
"github.com/spf13/cobra"
1416
"github.com/spf13/viper"
1517
)
1618

1719
var startCmd = &cobra.Command{
18-
Use: "start",
19-
Short: "Starts the microfab image running",
20+
Use: "start",
21+
Short: "Starts the microfab image running",
22+
GroupID: "mf",
2023
RunE: func(cmd *cobra.Command, args []string) error {
2124
return start()
2225
},
2326
}
2427

28+
var logs bool
29+
2530
func init() {
31+
startCmd.PersistentFlags().BoolVarP(&force, "force", "f", false, "Force restart if microfab already running")
32+
startCmd.PersistentFlags().BoolVarP(&logs, "logs", "l", false, "Display the logs (docker logs -f microfab)")
33+
34+
startCmd.PersistentFlags().StringVar(&cfg, "config", defaultCfg, "Microfab config")
35+
startCmd.PersistentFlags().StringVar(&cfgFile, "configFile", "", "Microfab config file")
36+
37+
startCmd.MarkFlagsMutuallyExclusive("config", "configFile")
38+
39+
viper.BindPFlag("MICROFAB_CONFIG", rootCmd.PersistentFlags().Lookup("config"))
2640

2741
}
2842

@@ -38,10 +52,9 @@ func start() error {
3852

3953
log.Printf("Starting microfab container..\n")
4054

41-
cfg = viper.GetString("MICROFAB_CONFIG")
42-
43-
if cfg == "" {
44-
return errors.Errorf("Can't start - config is blank")
55+
cfg, err = GetConfig()
56+
if err != nil {
57+
return errors.Wrapf(err, "Unable to determine config")
4558
}
4659

4760
env[0] = "FABRIC_LOGGING_SPEC=info"
@@ -96,6 +109,14 @@ func start() error {
96109

97110
log.Printf("Container ID %s\n", resp.ID)
98111
log.Printf("Microfab is up and running\n")
112+
if logs {
113+
out, err := cli.ContainerLogs(ctx, resp.ID, types.ContainerLogsOptions{ShowStdout: true, ShowStderr: true, Follow: true})
114+
if err != nil {
115+
return err
116+
}
117+
118+
stdcopy.StdCopy(os.Stdout, os.Stderr, out)
119+
}
99120

100121
return nil
101122
}

pkg/microfab/stop.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ import (
1212
)
1313

1414
var stopCmd = &cobra.Command{
15-
Use: "stop",
16-
Short: "Stops the microfab image running",
15+
Use: "stop",
16+
Short: "Stops the microfab image running",
17+
GroupID: "mf",
1718
RunE: func(cmd *cobra.Command, args []string) error {
1819
return Stop("microfab")
1920
},

0 commit comments

Comments
 (0)