Skip to content

Commit ee329db

Browse files
authored
Merge pull request #514 from oasisprotocol/matevz/feature/rofl-flags-cleanup
cmd/rofl: Revamp common rofl flags
2 parents 9cd64b3 + 216ebf1 commit ee329db

8 files changed

Lines changed: 111 additions & 131 deletions

File tree

cmd/rofl/build/build.go

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
buildRofl "github.com/oasisprotocol/cli/build/rofl"
2222
"github.com/oasisprotocol/cli/cmd/common"
2323
roflCommon "github.com/oasisprotocol/cli/cmd/rofl/common"
24-
cliConfig "github.com/oasisprotocol/cli/config"
2524
)
2625

2726
// Build modes.
@@ -31,24 +30,21 @@ const (
3130
)
3231

3332
var (
34-
outputFn string
35-
buildMode string
36-
offline bool
37-
noUpdate bool
38-
doVerify bool
39-
deploymentName string
40-
noDocker bool
41-
onlyValidate bool
33+
outputFn string
34+
buildMode string
35+
offline bool
36+
noUpdate bool
37+
doVerify bool
38+
noDocker bool
39+
onlyValidate bool
4240

4341
Cmd = &cobra.Command{
4442
Use: "build",
4543
Short: "Build a ROFL application",
4644
Args: cobra.NoArgs,
4745
RunE: func(cmd *cobra.Command, _ []string) error {
4846
cmd.SilenceUsage = true
49-
cfg := cliConfig.Global()
50-
npa := common.GetNPASelection(cfg)
51-
manifest, deployment := roflCommon.LoadManifestAndSetNPA(cfg, npa, deploymentName, &roflCommon.ManifestOptions{
47+
manifest, deployment, npa := roflCommon.LoadManifestAndSetNPA(&roflCommon.ManifestOptions{
5248
NeedAppID: true,
5349
NeedAdmin: false,
5450
})
@@ -64,7 +60,7 @@ var (
6460
}
6561

6662
fmt.Println("Building a ROFL application...")
67-
fmt.Printf("Deployment: %s\n", deploymentName)
63+
fmt.Printf("Deployment: %s\n", roflCommon.DeploymentName)
6864
fmt.Printf("Network: %s\n", deployment.Network)
6965
fmt.Printf("ParaTime: %s\n", deployment.ParaTime)
7066
fmt.Printf("Debug: %v\n", deployment.Debug)
@@ -124,7 +120,7 @@ var (
124120

125121
// Setup some build environment variables.
126122
os.Setenv("ROFL_MANIFEST", manifest.SourceFileName())
127-
os.Setenv("ROFL_DEPLOYMENT_NAME", deploymentName)
123+
os.Setenv("ROFL_DEPLOYMENT_NAME", roflCommon.DeploymentName)
128124
os.Setenv("ROFL_DEPLOYMENT_NETWORK", deployment.Network)
129125
os.Setenv("ROFL_DEPLOYMENT_PARATIME", deployment.ParaTime)
130126
os.Setenv("ROFL_TMPDIR", tmpDir)
@@ -157,7 +153,7 @@ var (
157153
runScript(manifest, buildRofl.ScriptBuildPost)
158154

159155
// Write the bundle out.
160-
outFn := roflCommon.GetOrcFilename(manifest, deploymentName)
156+
outFn := roflCommon.GetOrcFilename(manifest, roflCommon.DeploymentName)
161157
if outputFn != "" {
162158
outFn = outputFn
163159
}
@@ -260,7 +256,7 @@ var (
260256
fmt.Println("Update the manifest with the following identities to use the new app:")
261257
fmt.Println()
262258
fmt.Printf("deployments:\n")
263-
fmt.Printf(" %s:\n", deploymentName)
259+
fmt.Printf(" %s:\n", roflCommon.DeploymentName)
264260
fmt.Printf(" policy:\n")
265261
fmt.Printf(" enclaves:\n")
266262
for _, id := range ids {
@@ -366,10 +362,11 @@ func init() {
366362
buildFlags.StringVar(&outputFn, "output", "", "output bundle filename")
367363
buildFlags.BoolVar(&noUpdate, "no-update-manifest", false, "do not update the manifest")
368364
buildFlags.BoolVar(&doVerify, "verify", false, "verify build against manifest and on-chain state")
369-
buildFlags.StringVar(&deploymentName, "deployment", buildRofl.DefaultDeploymentName, "deployment name")
370365
buildFlags.BoolVar(&noDocker, "no-docker", false, "do not use the Dockerized builder")
371366
buildFlags.BoolVar(&onlyValidate, "only-validate", false, "validate without building")
372367

368+
buildFlags.AddFlagSet(roflCommon.DeploymentFlags)
369+
373370
// TODO: Remove when all the examples, demos and docs are updated.
374371
var dummy bool
375372
buildFlags.BoolVar(&dummy, "update-manifest", true, "not update the manifest")

cmd/rofl/common/flags.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package common
2+
3+
import (
4+
flag "github.com/spf13/pflag"
5+
6+
buildRofl "github.com/oasisprotocol/cli/build/rofl"
7+
)
8+
9+
var (
10+
// WipeFlags is the flag for wiping the machine storage on deployment/restart.
11+
WipeFlags *flag.FlagSet
12+
13+
// DeploymentFlags provide the deployment name.
14+
DeploymentFlags *flag.FlagSet
15+
16+
// DeploymentName is the name of the ROFL app deployment.
17+
DeploymentName string
18+
19+
// WipeStorage enables wiping the machine storage on deployment/restart.
20+
WipeStorage bool
21+
)
22+
23+
func init() {
24+
WipeFlags = flag.NewFlagSet("", flag.ContinueOnError)
25+
WipeFlags.BoolVar(&WipeStorage, "wipe-storage", false, "whether to wipe machine storage")
26+
27+
DeploymentFlags = flag.NewFlagSet("", flag.ContinueOnError)
28+
DeploymentFlags.StringVar(&DeploymentName, "deployment", buildRofl.DefaultDeploymentName, "deployment name")
29+
}

cmd/rofl/common/manifest.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77

88
"github.com/oasisprotocol/cli/build/rofl"
99
"github.com/oasisprotocol/cli/cmd/common"
10-
"github.com/oasisprotocol/cli/config"
10+
cliConfig "github.com/oasisprotocol/cli/config"
1111
)
1212

1313
// ManifestOptions configures the manifest options.
@@ -23,20 +23,23 @@ type ManifestOptions struct {
2323
// selection.
2424
//
2525
// In case there is an error in loading the manifest, it aborts the application.
26-
func LoadManifestAndSetNPA(cfg *config.Config, npa *common.NPASelection, deployment string, opts *ManifestOptions) (*rofl.Manifest, *rofl.Deployment) {
27-
manifest, d, err := MaybeLoadManifestAndSetNPA(cfg, npa, deployment, opts)
26+
func LoadManifestAndSetNPA(opts *ManifestOptions) (*rofl.Manifest, *rofl.Deployment, *common.NPASelection) {
27+
cfg := cliConfig.Global()
28+
npa := common.GetNPASelection(cfg)
29+
30+
manifest, d, err := MaybeLoadManifestAndSetNPA(cfg, npa, DeploymentName, opts)
2831
cobra.CheckErr(err)
2932
if opts != nil && opts.NeedAppID && !d.HasAppID() {
30-
cobra.CheckErr(fmt.Errorf("deployment '%s' does not have an app ID set, maybe you need to run `oasis rofl create`", deployment))
33+
cobra.CheckErr(fmt.Errorf("deployment '%s' does not have an app ID set, maybe you need to run `oasis rofl create`", DeploymentName))
3134
}
32-
return manifest, d
35+
return manifest, d, npa
3336
}
3437

3538
// MaybeLoadManifestAndSetNPA loads the ROFL app manifest and reconfigures the
3639
// network/paratime/account selection.
3740
//
3841
// In case there is an error in loading the manifest, it is returned.
39-
func MaybeLoadManifestAndSetNPA(cfg *config.Config, npa *common.NPASelection, deployment string, opts *ManifestOptions) (*rofl.Manifest, *rofl.Deployment, error) {
42+
func MaybeLoadManifestAndSetNPA(cfg *cliConfig.Config, npa *common.NPASelection, deployment string, opts *ManifestOptions) (*rofl.Manifest, *rofl.Deployment, error) {
4043
manifest, err := rofl.LoadManifest()
4144
if err != nil {
4245
return nil, nil, err

cmd/rofl/deploy.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,13 @@ var (
4949
Short: "Deploy ROFL to a specified machine",
5050
Args: cobra.NoArgs,
5151
Run: func(_ *cobra.Command, _ []string) {
52-
cfg := cliConfig.Global()
53-
npa := common.GetNPASelection(cfg)
5452
txCfg := common.GetTransactionConfig()
5553

5654
if txCfg.Offline {
5755
cobra.CheckErr("offline mode currently not supported")
5856
}
5957

60-
manifest, deployment := roflCommon.LoadManifestAndSetNPA(cfg, npa, deploymentName, &roflCommon.ManifestOptions{
58+
manifest, deployment, npa := roflCommon.LoadManifestAndSetNPA(&roflCommon.ManifestOptions{
6159
NeedAppID: true,
6260
NeedAdmin: true,
6361
})
@@ -105,15 +103,15 @@ var (
105103
if roflServices, ok := provider.DefaultRoflServices[npa.ParaTime.ID]; ok {
106104
deployProvider = roflServices.Provider
107105
} else {
108-
cobra.CheckErr(fmt.Sprintf("Provider not configured for deployment '%s' machine '%s'. Please specify --provider.", deploymentName, deployMachine))
106+
cobra.CheckErr(fmt.Sprintf("Provider not configured for deployment '%s' machine '%s'. Please specify --provider.", roflCommon.DeploymentName, deployMachine))
109107
}
110108
}
111109

112110
machine.Provider = deployProvider
113111
default:
114112
// Already set, require the provider to be omitted or equal.
115113
if deployProvider != "" && deployProvider != machine.Provider {
116-
cobra.CheckErr(fmt.Sprintf("Provider '%s' conflicts with existing provider '%s' for deployment '%s' machine '%s'. Omit --provider.", deployProvider, machine.Provider, deploymentName, deployMachine))
114+
cobra.CheckErr(fmt.Sprintf("Provider '%s' conflicts with existing provider '%s' for deployment '%s' machine '%s'. Omit --provider.", deployProvider, machine.Provider, roflCommon.DeploymentName, deployMachine))
117115
}
118116
}
119117

@@ -132,7 +130,7 @@ var (
132130
}
133131

134132
ociRepository := ociRepository(deployment)
135-
orcFilename := roflCommon.GetOrcFilename(manifest, deploymentName)
133+
orcFilename := roflCommon.GetOrcFilename(manifest, roflCommon.DeploymentName)
136134
fmt.Printf("Pushing ROFL app to OCI repository '%s'...\n", ociRepository)
137135
ociDigest, manifestHash := pushBundleToOciRepository(orcFilename, ociRepository)
138136
// Save the OCI repository field to the configuration file so we avoid multiple uploads.
@@ -200,7 +198,7 @@ var (
200198
TermCount: deployTermCount,
201199
})
202200

203-
acc := common.LoadAccount(cfg, npa.AccountName)
201+
acc := common.LoadAccount(cliConfig.Global(), npa.AccountName)
204202
var sigTx, meta any
205203
sigTx, meta, err = common.SignParaTimeTransaction(ctx, npa, acc, conn, tx, nil)
206204
cobra.CheckErr(err)
@@ -260,12 +258,12 @@ var (
260258
Method: scheduler.MethodDeploy,
261259
Args: cbor.Marshal(scheduler.DeployRequest{
262260
Deployment: machineDeployment,
263-
WipeStorage: false,
261+
WipeStorage: roflCommon.WipeStorage,
264262
}),
265263
})},
266264
})
267265

268-
acc := common.LoadAccount(cfg, npa.AccountName)
266+
acc := common.LoadAccount(cliConfig.Global(), npa.AccountName)
269267
var sigTx, meta any
270268
sigTx, meta, err = common.SignParaTimeTransaction(ctx, npa, acc, conn, tx, nil)
271269
cobra.CheckErr(err)
@@ -455,8 +453,8 @@ func init() {
455453
providerFlags.BoolVar(&deployShowOffers, "show-offers", false, "show all provider offers and quit")
456454
providerFlags.BoolVar(&deployReplaceMachine, "replace-machine", false, "rent a new machine if the provided one expired")
457455

458-
deployCmd.Flags().AddFlagSet(common.SelectorFlags)
459456
deployCmd.Flags().AddFlagSet(common.RuntimeTxFlags)
460-
deployCmd.Flags().AddFlagSet(deploymentFlags)
461457
deployCmd.Flags().AddFlagSet(providerFlags)
458+
deployCmd.Flags().AddFlagSet(roflCommon.DeploymentFlags)
459+
deployCmd.Flags().AddFlagSet(roflCommon.WipeFlags)
462460
}

cmd/rofl/machine/logs.go

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,13 @@ import (
66
"time"
77

88
"github.com/spf13/cobra"
9-
flag "github.com/spf13/pflag"
109

1110
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/client"
1211
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/connection"
1312
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/crypto/signature"
1413
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/crypto/signature/ed25519"
1514
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/types"
1615

17-
buildRofl "github.com/oasisprotocol/cli/build/rofl"
1816
"github.com/oasisprotocol/cli/build/rofl/scheduler"
1917
"github.com/oasisprotocol/cli/cmd/common"
2018
roflCommon "github.com/oasisprotocol/cli/cmd/rofl/common"
@@ -26,10 +24,7 @@ var logsCmd = &cobra.Command{
2624
Short: "Show logs from the given machine",
2725
Args: cobra.MaximumNArgs(1),
2826
Run: func(_ *cobra.Command, args []string) {
29-
cfg := cliConfig.Global()
30-
npa := common.GetNPASelection(cfg)
31-
32-
_, deployment := roflCommon.LoadManifestAndSetNPA(cfg, npa, deploymentName, &roflCommon.ManifestOptions{
27+
_, deployment, npa := roflCommon.LoadManifestAndSetNPA(&roflCommon.ManifestOptions{
3328
NeedAppID: true,
3429
NeedAdmin: false,
3530
})
@@ -70,7 +65,7 @@ var logsCmd = &cobra.Command{
7065
cobra.CheckErr(err)
7166

7267
// TODO: Cache authentication token so we don't need to re-authenticate.
73-
acc := common.LoadAccount(cfg, npa.AccountName)
68+
acc := common.LoadAccount(cliConfig.Global(), npa.AccountName)
7469

7570
sigCtx := &signature.RichContext{
7671
RuntimeID: npa.ParaTime.Namespace(),
@@ -92,9 +87,6 @@ var logsCmd = &cobra.Command{
9287
}
9388

9489
func init() {
95-
deploymentFlags := flag.NewFlagSet("", flag.ContinueOnError)
96-
deploymentFlags.StringVar(&deploymentName, "deployment", buildRofl.DefaultDeploymentName, "deployment name")
97-
98-
logsCmd.Flags().AddFlagSet(deploymentFlags)
90+
logsCmd.Flags().AddFlagSet(roflCommon.DeploymentFlags)
9991
logsCmd.Flags().AddFlagSet(common.AnswerYesFlag)
10092
}

0 commit comments

Comments
 (0)