Skip to content

Commit

Permalink
process-user-data: move logic in pkgs
Browse files Browse the repository at this point in the history
- Move the logic into pkgs
- Use the log module for log output

Signed-off-by: Magnus Kulke <magnuskulke@microsoft.com>
  • Loading branch information
mkulke committed Jan 24, 2024
1 parent 72ea7fa commit e025ced
Show file tree
Hide file tree
Showing 10 changed files with 736 additions and 722 deletions.
64 changes: 36 additions & 28 deletions cmd/process-user-data/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,22 @@ import (
"os"

cmdUtil "github.com/confidential-containers/cloud-api-adaptor/cmd"
"github.com/confidential-containers/cloud-api-adaptor/pkg/agent"
daemon "github.com/confidential-containers/cloud-api-adaptor/pkg/forwarder"
"github.com/confidential-containers/cloud-api-adaptor/pkg/userdata"
"github.com/spf13/cobra"
)

const (
programName = "process-user-data"
providerAzure = "azure"
providerAws = "aws"

defaultAgentConfigPath = "/etc/agent-config.toml"
defaultAuthJsonPath = "/run/peerpod/auth.json"
)

var versionFlag bool
var rootCmd = &cobra.Command{
Use: programName,
Short: "A program to process user data and update agent config file",
Expand All @@ -23,40 +35,36 @@ var rootCmd = &cobra.Command{
},
}

var versionFlag bool
var provisionFilesCmd = &cobra.Command{
Use: "provision-files",
Short: "Provision required files based on user data",
RunE: provisionFiles,
SilenceUsage: true, // Silence usage on error
}

var updateAgentConfigCmd = &cobra.Command{
Use: "update-agent-config",
Short: "Update the agent configuration file",
RunE: updateAgentConfig,
SilenceUsage: true, // Silence usage on error
}

var cfg Config

func init() {
// set a fixed value for authJsonPath
cfg.authJsonPath = defaultAuthJsonFilePath
var agentConfigPath, daemonConfigPath string
var fetchTimeout int

rootCmd.PersistentFlags().BoolVarP(&versionFlag, "version", "v", false, "Print the version")
// Add a flag to specify the daemonConfigPath
rootCmd.PersistentFlags().StringVarP(&cfg.daemonConfigPath, "daemon-config-path", "d", daemon.DefaultConfigPath, "Path to a daemon config file")

// Add a flag to specify the timeout for fetching user data
provisionFilesCmd.Flags().IntVarP(&cfg.userDataFetchTimeout, "user-data-fetch-timeout", "t", 180, "Timeout (in secs) for fetching user data")
rootCmd.PersistentFlags().StringVarP(&daemonConfigPath, "daemon-config-path", "d", daemon.DefaultConfigPath, "Path to a daemon config file")

var provisionFilesCmd = &cobra.Command{
Use: "provision-files",
Short: "Provision required files based on user data",
RunE: func(_ *cobra.Command, _ []string) error {
cfg := userdata.NewConfig(defaultAuthJsonPath, daemonConfigPath, fetchTimeout)
return userdata.ProvisionFiles(cfg)
},
SilenceUsage: true, // Silence usage on error
}
provisionFilesCmd.Flags().IntVarP(&fetchTimeout, "user-data-fetch-timeout", "t", 180, "Timeout (in secs) for fetching user data")
rootCmd.AddCommand(provisionFilesCmd)

// Add a flag to specify the agentConfigPath to updateAgentConfigCmd subcommand
updateAgentConfigCmd.Flags().StringVarP(&cfg.agentConfigPath, "agent-config-file", "a", defaultAgentConfigPath, "Path to a agent config file")

var updateAgentConfigCmd = &cobra.Command{
Use: "update-agent-config",
Short: "Update the agent configuration file",
RunE: func(_ *cobra.Command, _ []string) error {
cfg := agent.NewConfig(agentConfigPath, defaultAuthJsonPath, daemonConfigPath)
return agent.UpdateConfig(cfg)
},
SilenceUsage: true, // Silence usage on error
}
updateAgentConfigCmd.Flags().StringVarP(&agentConfigPath, "agent-config-file", "a", defaultAgentConfigPath, "Path to a agent config file")
rootCmd.AddCommand(updateAgentConfigCmd)

}

func main() {
Expand Down
Loading

0 comments on commit e025ced

Please sign in to comment.