Skip to content

Commit

Permalink
added beacon and relay_eth, yet to test
Browse files Browse the repository at this point in the history
  • Loading branch information
supragya committed Jan 2, 2021
1 parent 23345a5 commit d482763
Show file tree
Hide file tree
Showing 23 changed files with 1,950 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -1,53 +1,50 @@
/*
Copyright © 2020 NAME HERE <EMAIL ADDRESS>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package actions

import (
"errors"
"os"

"github.com/marlinprotocol/ctl2/modules/registry"
"github.com/marlinprotocol/ctl2/modules/util"
"github.com/marlinprotocol/ctl2/types"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

"github.com/marlinprotocol/ctl2/modules/registry"
"github.com/marlinprotocol/ctl2/modules/util"
"github.com/marlinprotocol/ctl2/types"
var (
enableBeta bool
runtime string
version string
instanceId string
projectId string = "beacon"
updatePolicy string
skipChecksum bool
forceRuntime bool
runtimeArgs map[string]string
)

// AppCmd represents the registry command
var ConfigureCmd = &cobra.Command{
Use: "configure",
Short: "Configure iris gateway",
Long: `Configure iris gateway`,
Run: func(cmd *cobra.Command, args []string) {
if err := setupConfiguration(enableBeta, forceRuntime, updatePolicy, runtime, version); err != nil {
log.Error("Error while setting up config: ", err)
} else {
log.Info("Config setup successfully")
var ConfigTest = func(cmd *cobra.Command, args []string) error {
var marlinConfig types.Project
err := viper.UnmarshalKey("marlinctl", &marlinConfig)
if err != nil {
return err
}
if !viper.IsSet("beacon") {
log.Debug("Setting up default config for running beacon.")
updPol, ok1 := marlinConfig.AdditionalInfo["defaultprojectupdatepolicy"]
defRun, ok2 := marlinConfig.AdditionalInfo["defaultprojectruntime"]
if ok1 && ok2 {
setupConfiguration(false,
false,
updPol.(string),
defRun.(string),
"latest")
}
},
}

func init() {
ConfigureCmd.Flags().StringVarP(&updatePolicy, "update-policy", "u", "minor", "update policy to enforce - major / minor / patch / none")
ConfigureCmd.Flags().BoolVarP(&enableBeta, "enable-beta", "b", false, "enable beta releases")
ConfigureCmd.Flags().StringVarP(&version, "version", "v", "latest", "Version to run")
ConfigureCmd.Flags().StringVarP(&runtime, "runtime", "r", "", "Application runtime")
ConfigureCmd.Flags().BoolVarP(&forceRuntime, "force-runtime", "f", false, "Forcefully set application runtime")
} else {
log.Debug("Project config found. Not creating defaults.")
}
return nil
}

func setupConfiguration(enableBeta bool, forceRuntime bool, updatePolicy string, runtime string, version string) error {
Expand Down
88 changes: 88 additions & 0 deletions cmd/beacon/actions/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
Copyright © 2020 NAME HERE <EMAIL ADDRESS>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package actions

import (
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/marlinprotocol/ctl2/modules/registry"
projectRunners "github.com/marlinprotocol/ctl2/modules/runner/beacon"
"github.com/marlinprotocol/ctl2/types"
)

// AppCmd represents the registry command
var CreateCmd = &cobra.Command{
Use: "create",
Short: `Create a beacon on local system`,
PreRunE: ConfigTest,
Run: func(cmd *cobra.Command, args []string) {
var projectConfig types.Project
err := viper.UnmarshalKey(projectId, &projectConfig)
if err != nil {
log.Error("Error while reading project config: ", err)
return
}
versionToRun, err := registry.GlobalRegistry.GetVersionToRun(projectId, updatePolicy, version)
if err != nil {
log.Error("Error while getting version to run: ", err)
return
}

runner, err := projectRunners.GetRunnerInstance(versionToRun.RunnerId, versionToRun.Version, projectConfig.Storage, versionToRun.RunnerData, false, skipChecksum, instanceId)
if err != nil {
log.Error("Cannot get runner: ", err.Error())
return
}

err = runner.PreRunSanity()
if err != nil {
log.Error("Failure during pre run sanity: ", err.Error())
return
}

err = runner.Prepare()
if err != nil {
log.Error("Failure during preparation: ", err.Error())
return
}

err = runner.Create(runtimeArgs)
if err != nil {
log.Error("Failure during start: ", err.Error())
return
}

projectConfig.CurrentVersion = versionToRun.Version

viper.Set(projectId, projectConfig)
err = viper.WriteConfig()
if err != nil {
log.Error("Failure while updating config for current version: ", err.Error())
return
}
},
}

func init() {
runtimeArgs = make(map[string]string)
CreateCmd.Flags().StringVarP(&version, "runtime-version", "x", "", "version override")
CreateCmd.Flags().StringVarP(&updatePolicy, "update-policy", "u", "", "update policy override")
CreateCmd.Flags().StringVarP(&instanceId, "instance-id", "i", "001", "instance-id of the resource")
CreateCmd.Flags().BoolVarP(&skipChecksum, "skip-checksum", "s", false, "skips checking file integrity during run")
CreateCmd.Flags().StringToStringVarP(&runtimeArgs, "runtime-arguments", "r", map[string]string{}, "runtime arguments for beacon")
}
103 changes: 103 additions & 0 deletions cmd/beacon/actions/destroy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
Copyright © 2020 NAME HERE <EMAIL ADDRESS>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package actions

import (
"encoding/json"
"errors"
"io/ioutil"
"os"

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"

projectRunners "github.com/marlinprotocol/ctl2/modules/runner/beacon"
"github.com/marlinprotocol/ctl2/types"
)

// AppCmd represents the registry command
var DestroyCmd = &cobra.Command{
Use: "destroy",
Short: "Destroy any running beacon",
Long: `Destroy any running beacon`,
PreRunE: ConfigTest,
Run: func(cmd *cobra.Command, args []string) {
var projectConfig types.Project
err := viper.UnmarshalKey(projectId, &projectConfig)
if err != nil {
log.Error("Error while reading project config: ", err)
os.Exit(1)
}

runnerId, version, err := getResourceMetaData(projectConfig, instanceId)
if err != nil {
log.Error("Error while fetching resource information: ", err)
os.Exit(1)
}

runner, err := projectRunners.GetRunnerInstance(runnerId, version, projectConfig.Storage, struct{}{}, true, true, instanceId)
if err != nil {
log.Error("Cannot get runner: ", err.Error())
os.Exit(1)
}

err = runner.PreRunSanity()
if err != nil {
log.Error("Failure during pre run sanity: ", err.Error())
return
}

err = runner.Destroy()
if err != nil {
log.Error("Failure during destroy: ", err.Error())
log.Warning("Destroy failure can occur when creation and destruction of processes is done manually and not all through marlinctl." +
" Failure may not reflect current process state.")
return
}

err = runner.PostRun()
if err != nil {
log.Error("Failure during post run: ", err.Error())
return
}
},
}

func init() {
DestroyCmd.Flags().StringVarP(&instanceId, "instance-id", "i", "001", "instance-id of the resource")
}

func getResourceMetaData(projectConfig types.Project, instanceId string) (string, string, error) {
resFileLocation := projectRunners.GetResourceFileLocation(projectConfig.Storage, instanceId)
if _, err := os.Stat(resFileLocation); os.IsNotExist(err) {
return "", "", errors.New("Cannot locate resource: " + resFileLocation)
}
file, err := ioutil.ReadFile(resFileLocation)
if err != nil {
return "", "", err
}
var resourceMetaData = struct {
Runner string `json:"Runner"`
Version string `json:"Version"`
}{}
err = json.Unmarshal([]byte(file), &resourceMetaData)
if err != nil {
return "", "", err
}
log.Debug("Resource metadata: ", resourceMetaData)
return resourceMetaData.Runner, resourceMetaData.Version, nil
}
54 changes: 54 additions & 0 deletions cmd/beacon/actions/listversions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
Copyright © 2020 NAME HERE <EMAIL ADDRESS>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package actions

import (
"os"

"github.com/marlinprotocol/ctl2/modules/registry"
"github.com/marlinprotocol/ctl2/types"
log "github.com/sirupsen/logrus"

"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// AppCmd represents the registry command
var ListVersionsCmd = &cobra.Command{
Use: "listversions",
Short: "List versions for beacon",
Long: `List versions for beacon`,
PreRunE: ConfigTest,
Run: func(cmd *cobra.Command, args []string) {
var projectConfig types.Project
err := viper.UnmarshalKey(projectId, &projectConfig)
if err != nil {
log.Error("Error while reading project config: ", err)
os.Exit(1)
}
versions, err := registry.GlobalRegistry.GetVersions(projectId, projectConfig.Subscription, "0.0.0", "major", projectConfig.Runtime)

if err != nil {
log.Error("Error encountered while listing versions: ", err)
return
}

registry.GlobalRegistry.PrettyPrintProjectVersions(versions)
},
}

func init() {
}
Loading

0 comments on commit d482763

Please sign in to comment.