-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Restructure all ops code into cmd package
Currently ops code is becoming bigger, as we are adding support for more commands. Breaking it down so that we maintain modularity.
- Loading branch information
1 parent
a6102e7
commit 4d7186e
Showing
13 changed files
with
889 additions
and
777 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
api "github.com/nanovms/ops/lepton" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func buildCommandHandler(cmd *cobra.Command, args []string) { | ||
targetRoot, _ := cmd.Flags().GetString("target-root") | ||
provider, _ := cmd.Flags().GetString("target-cloud") | ||
|
||
config, _ := cmd.Flags().GetString("config") | ||
config = strings.TrimSpace(config) | ||
c := unWarpConfig(config) | ||
c.Program = args[0] | ||
c.TargetRoot = targetRoot | ||
setDefaultImageName(cmd, c) | ||
|
||
p := getCloudProvider(provider) | ||
|
||
ctx := api.NewContext(c, &p) | ||
prepareImages(c) | ||
if _, err := p.BuildImage(ctx); err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
fmt.Printf("Bootable image file:%s\n", c.RunConfig.Imagename) | ||
} | ||
|
||
func BuildCommand() *cobra.Command { | ||
var config string | ||
var targetRoot string | ||
var targetCloud string | ||
var imageName string | ||
|
||
var cmdBuild = &cobra.Command{ | ||
Use: "build [ELF file]", | ||
Short: "Build an image from ELF", | ||
Args: cobra.MinimumNArgs(1), | ||
Run: buildCommandHandler, | ||
} | ||
|
||
cmdBuild.PersistentFlags().StringVarP(&config, "config", "c", "", "ops config file") | ||
cmdBuild.PersistentFlags().StringVarP(&targetRoot, "target-root", "r", "", "target root") | ||
cmdBuild.PersistentFlags().StringVarP(&targetCloud, "target-cloud", "t", "gcp", "cloud platform[gcp, onprem]") | ||
cmdBuild.PersistentFlags().StringVarP(&imageName, "imagename", "i", "", "image name") | ||
return cmdBuild | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
api "github.com/nanovms/ops/lepton" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func imageCommandHandler(cmd *cobra.Command, args []string) { | ||
if _, ok := os.LookupEnv("GOOGLE_APPLICATION_CREDENTIALS"); !ok { | ||
fmt.Printf(api.ErrorColor, "error: GOOGLE_APPLICATION_CREDENTIALS not set.\n") | ||
fmt.Printf(api.ErrorColor, "Follow https://cloud.google.com/storage/docs/reference/libraries to set it up.\n") | ||
os.Exit(1) | ||
} | ||
|
||
provider, _ := cmd.Flags().GetString("target-cloud") | ||
config, _ := cmd.Flags().GetString("config") | ||
config = strings.TrimSpace(config) | ||
|
||
c := unWarpConfig(config) | ||
c.Program = args[0] | ||
|
||
// override config from command line | ||
if len(provider) > 0 { | ||
c.CloudConfig.Platform = provider | ||
} | ||
|
||
if len(c.CloudConfig.Platform) == 0 { | ||
fmt.Printf(api.ErrorColor, "error: Please select on of the cloud platform in config. [onprem, gcp]") | ||
os.Exit(1) | ||
} | ||
|
||
if len(c.CloudConfig.ProjectID) == 0 { | ||
fmt.Printf(api.ErrorColor, "error: Please specifiy a cloud projectid in config.\n") | ||
os.Exit(1) | ||
} | ||
|
||
if len(c.CloudConfig.BucketName) == 0 { | ||
fmt.Printf(api.ErrorColor, "error: Please specifiy a cloud bucket in config.\n") | ||
os.Exit(1) | ||
} | ||
|
||
setDefaultImageName(cmd, c) | ||
|
||
p := getCloudProvider(provider) | ||
ctx := api.NewContext(c, &p) | ||
prepareImages(c) | ||
|
||
archpath, err := p.BuildImage(ctx) | ||
if err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
|
||
gcloud := p.(*api.GCloud) | ||
err = gcloud.Storage.CopyToBucket(c, archpath) | ||
if err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
err = gcloud.CreateImage(ctx) | ||
if err != nil { | ||
fmt.Println(err) | ||
} else { | ||
imageName := fmt.Sprintf("nanos-%v-image", filepath.Base(c.Program)) | ||
fmt.Printf("gcp image '%s' created...\n", imageName) | ||
} | ||
} | ||
|
||
func ImageCommands() *cobra.Command { | ||
var targetCloud string | ||
var config string | ||
var cmdImageCreate = &cobra.Command{ | ||
Use: "create", | ||
Short: "create nanos image from ELF", | ||
Args: cobra.MinimumNArgs(1), | ||
Run: imageCommandHandler, | ||
} | ||
|
||
cmdImageCreate.PersistentFlags().StringVarP(&targetCloud, "target-cloud", "t", "gcp", "cloud platform [gcp, onprem]") | ||
cmdImageCreate.PersistentFlags().StringVarP(&config, "config", "c", "", "ops config file") | ||
|
||
var cmdImage = &cobra.Command{ | ||
Use: "image", | ||
Short: "manage nanos images", | ||
ValidArgs: []string{"create"}, | ||
Args: cobra.OnlyValidArgs, | ||
} | ||
return cmdImage | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
api "github.com/nanovms/ops/lepton" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func instanceCommandHandler(cmd *cobra.Command, args []string) { | ||
if _, ok := os.LookupEnv("GOOGLE_APPLICATION_CREDENTIALS"); !ok { | ||
fmt.Printf(api.ErrorColor, "error: GOOGLE_APPLICATION_CREDENTIALS not set.\n") | ||
fmt.Printf(api.ErrorColor, "Follow https://cloud.google.com/storage/docs/reference/libraries to set it up.\n") | ||
os.Exit(1) | ||
} | ||
provider, _ := cmd.Flags().GetString("target-cloud") | ||
|
||
projectID, err := cmd.Flags().GetString("projectid") | ||
if err != nil || len(projectID) == 0 { | ||
fmt.Printf(api.ErrorColor, "error: Not a valid ProjectID.\n") | ||
os.Exit(1) | ||
} | ||
|
||
config, _ := cmd.Flags().GetString("config") | ||
config = strings.TrimSpace(config) | ||
c := unWarpConfig(config) | ||
c.CloudConfig.ImageName = strings.Trim(args[0], " ") | ||
c.CloudConfig.ProjectID = projectID | ||
|
||
p := getCloudProvider(provider) | ||
ctx := api.NewContext(c, &p) | ||
gcloud := p.(*api.GCloud) | ||
err = gcloud.CreateInstance(ctx) | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
} | ||
|
||
// InstanceCommands provided instance related commands | ||
func InstanceCommands() *cobra.Command { | ||
var projectID string | ||
var targetCloud string | ||
var cmdInstance = &cobra.Command{ | ||
Use: "instance", | ||
Short: "manage nanos instances", | ||
ValidArgs: []string{"new"}, | ||
Args: cobra.OnlyValidArgs, | ||
} | ||
|
||
var cmdInstanceNew = &cobra.Command{ | ||
Use: "new [imagename]", | ||
Short: "new nanos instance", | ||
Args: cobra.MinimumNArgs(1), | ||
Run: instanceCommandHandler, | ||
} | ||
cmdInstanceNew.PersistentFlags().StringVarP(&targetCloud, "target-cloud", "t", "gcp", "cloud platform [gcp, onprem]") | ||
cmdInstanceNew.PersistentFlags().StringVarP(&projectID, "projectid", "n", "", "ProjectID") | ||
cmdInstance.AddCommand(cmdInstanceNew) | ||
return cmdInstance | ||
} |
Oops, something went wrong.