Skip to content

Commit

Permalink
template command and fix toscalib normative types
Browse files Browse the repository at this point in the history
  • Loading branch information
dciangot committed Feb 17, 2020
1 parent e55897e commit 7b216f4
Show file tree
Hide file tree
Showing 38 changed files with 540 additions and 124 deletions.
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ install:

tidy:
$(GOCMD) mod tidy
$(GOCMD) mod vendor

docker-bin-build:
docker run --rm -it -v ${PWD}:/go -w /go/ golang:1.13.6 go build -mod vendor -o "$(BINARY_NAME)" -v
Expand All @@ -57,10 +58,10 @@ docker-img-build:
docker build . -t dodas

windows-build:
env GOOS=windows CGO_ENABLED=0 $(GOBUILD) -o $(BINARY_NAME).exe -v
env GOOS=windows CGO_ENABLED=0 $(GOBUILD) -mod vendor -o $(BINARY_NAME).exe -v

macos-build:
env GOOS=darwin CGO_ENABLED=0 $(GOBUILD) -o $(BINARY_NAME)_osx -v
env GOOS=darwin CGO_ENABLED=0 $(GOBUILD) -mod vendor -o $(BINARY_NAME)_osx -v

gensrc:
rm -f $(VERSIONFILE)
Expand Down
18 changes: 7 additions & 11 deletions cmd/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,23 @@ import (
func MakeApps() *cobra.Command {
var command = &cobra.Command{
Use: "app",
Short: "Install Kubernetes apps from helm charts or YAML files",
Short: "Install Kubernetes cluster and apps from helm charts or YAML files",
Long: `Install Kubernetes apps from helm charts or YAML files using the "install"
command. Helm 2 is used by default unless a --helm3 flag exists and is passed.
You can also find the post-install message for each app with the "info"
command.`,
Example: ` k3sup app install
k3sup app info inlets-operator`,
Example: ` dodas app install`,
SilenceUsage: false,
}

var install = &cobra.Command{
Use: "install",
Short: "Install a Kubernetes app",
Example: ` k3sup app install [APP]
k3sup app install openfaas --help
k3sup app install inlets-operator --token-file $HOME/do
k3sup app install --help`,
Short: "Install a DODAS cluster with Kubernetes apps",
Example: ` dodas app install [APP]
dodas app install cod --x509-cert $HOME/do
dodas app install --help`,
SilenceUsage: true,
}

install.PersistentFlags().String("kubeconfig", "kubeconfig", "Local path for your kubeconfig file")
//install.PersistentFlags().String("kubeconfig", "kubeconfig", "Local path for your kubeconfig file")

install.RunE = func(command *cobra.Command, args []string) error {

Expand Down
128 changes: 91 additions & 37 deletions cmd/apps/cod_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"bytes"
"fmt"
"html/template"
"io/ioutil"
"strings"

"github.com/Masterminds/sprig"
"github.com/spf13/cobra"
Expand All @@ -13,48 +15,90 @@ import (

// MakeInstallCOD ..
func MakeInstallCOD(clientConf *utils.Conf) *cobra.Command {
var minio = &cobra.Command{
Use: "minio",
Short: "Install minio",
Long: `Install minio`,
Example: ` k3sup app install minio`,
var cod *cobra.Command = &cobra.Command{
Use: "cod",
Short: "Install CachingOnDemand",
Long: ``,
Example: `dodas app install cod`,
SilenceUsage: true,
}

minio.Flags().Bool("update-repo", true, "Update the helm repo")
minio.Flags().String("access-key", "", "Provide an access key to override the pre-generated value")
minio.Flags().String("secret-key", "", "Provide a secret key to override the pre-generated value")
minio.Flags().Bool("distributed", false, "Deploy Minio in Distributed Mode")
minio.Flags().String("namespace", "default", "Kubernetes namespace for the application")
minio.Flags().Bool("persistence", false, "Enable persistence")
minio.Flags().StringArray("set", []string{},
cod.Flags().Int("nslaves", 2, "Number of cache servers")
cod.Flags().Bool("gsi-enabled", false, "Enable GSI-VO auth")
cod.Flags().String("origin-host", "xrootd-cms.infn.it", "Origin server host")
cod.Flags().Int("origin-port", 1094, "Origin server port")
cod.Flags().String("redirector-host", "", "Cache redirector host")
cod.Flags().Int("redirector-port", 31213, "Cache redirector port")
cod.Flags().String("voms-file", "", "Provide a voms file for a vo (example --voms-file cms.txt=/tmp/voms/cms.txt)")
cod.Flags().String("x509-cert", "", "path to the certificate for cache server auth with remote")
cod.Flags().String("x509-key", "", "path to the certificate key for cache server auth with remote")
cod.Flags().StringArray("set", []string{},
"Use custom flags or override existing flags \n(example --set persistence.enabled=true)")

minio.RunE = func(command *cobra.Command, args []string) error {
// .... https://github.com/alexellis/k3sup/blob/master/cmd/apps/minio_app.go
// https://github.com/alexellis/k3sup/blob/master/cmd/apps/kubernetes_exec.go

// Get path
vomsFile := Voms{
Name: "vomsCMS",
Content: `
asdasdad
sadasdad`,
cod.RunE = func(command *cobra.Command, args []string) error {

gsiEnabled, _ := cod.Flags().GetBool("gsi-enabled")
vomsFileConf, _ := cod.Flags().GetString("voms-file")
certFileConf, _ := cod.Flags().GetString("x509-cert")
keyFileConf, _ := cod.Flags().GetString("x509-key")
redirectorHost, _ := cod.Flags().GetString("redirector-host")
originHost, _ := cod.Flags().GetString("origin-host")
redirectorPort, _ := cod.Flags().GetInt("redirector-port")
originPort, _ := cod.Flags().GetInt("origin-port")
nslaves, _ := cod.Flags().GetInt("nslaves")

var params CoDParams
var slaves = SlavesStruct{
SlaveNum: nslaves,
}

cert := `
mycert`

key := `
mykey`
if gsiEnabled {
vomsFileStrings := strings.Split(vomsFileConf, "=")
if len(vomsFileStrings) < 2 {
return fmt.Errorf("Invalid voms file format. Please use e.g.: --voms-file cms.txt=/tmp/voms/cms.txt")
}

vomsContent, err := ioutil.ReadFile(vomsFileStrings[1])
if err != nil {
return fmt.Errorf("Failed to read voms file: %s", err)
}

vomsFile := Voms{
Name: vomsFileStrings[0],
Content: string(vomsContent),
}

cert, err := ioutil.ReadFile(certFileConf)
if err != nil {
return fmt.Errorf("Failed to read X509 cert file: %s", err)
}
key, err := ioutil.ReadFile(keyFileConf)
if err != nil {
return fmt.Errorf("Failed to read X509 cert key file: %s", err)
}

params = CoDParams{
Slaves: slaves,
ExternalIP: "{{ externalIp }}",
VomsFile: vomsFile,
GsiEnabled: gsiEnabled,
CacheCert: string(cert),
CacheKey: string(key),
RedirectorHost: redirectorHost,
RedirectorPort: redirectorPort,
OriginHost: originHost,
OriginPort: originPort,
}
}

var params = CoDParams{
SlaveNum: 2,
ExternalIP: "{{ externalIp }}",
VomsFile: vomsFile,
CacheCert: cert,
CacheKey: key,
Redirector: "myredirector.com",
params = CoDParams{
Slaves: slaves,
GsiEnabled: gsiEnabled,
ExternalIP: "{{ externalIp }}",
RedirectorHost: redirectorHost,
RedirectorPort: redirectorPort,
OriginHost: originHost,
OriginPort: originPort,
}

// Create a new template and parse the letter into it.
Expand All @@ -66,7 +110,7 @@ mykey`
return fmt.Errorf("Failed to compile the template: %s", err)
}

fmt.Println(b.String())
//fmt.Println(b.String())

err = clientConf.Validate(b.Bytes())
if err != nil {
Expand All @@ -78,10 +122,20 @@ mykey`
return err
}

fmt.Printf("InfID: %s", infID)
tempFile, err := ioutil.TempFile("/tmp/", "cod_"+infID+"_")
defer tempFile.Close()
if err != nil {
return err
}

fmt.Printf("Writing compiled template in: %s", tempFile.Name())
_, err = tempFile.Write(b.Bytes())
if err != nil {
return err
}

return nil
}

return minio
return cod
}
56 changes: 42 additions & 14 deletions cmd/apps/cod_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,27 @@ type Voms struct {
Content string
}

// SlavesStruct ..
type SlavesStruct struct {
SlaveNum int
SlaveMemGB int
SlaveCPUs int
OsImage string
}

// CoDParams ..
type CoDParams struct {
SlaveNum int
ExternalIP string
VO string
VomsFile Voms
CacheCert string
CacheKey string
Redirector string
Slaves SlavesStruct
GsiEnabled bool
ExternalIP string
VO string
VomsFile Voms
CacheCert string
CacheKey string
RedirectorHost string
RedirectorPort int
OriginHost string
OriginPort int
}

// CoDTemplate ..
Expand Down Expand Up @@ -44,7 +56,7 @@ topology_template:
number_of_slaves:
type: integer
default: {{ .SlaveNum }}
default: {{ .Slaves.SlaveNum }}
num_cpus_slave:
type: integer
Expand All @@ -57,8 +69,8 @@ topology_template:
server_image:
type: string
#default: "ost://openstack.fisica.unipg.it/cb87a2ac-5469-4bd5-9cce-9682c798b4e4"
#default: "ost://horizon.cloud.cnaf.infn.it/3d993ab8-5d7b-4362-8fd6-af1391edca39"
default: "ost://cloud.recas.ba.infn.it/1113d7e8-fc5d-43b9-8d26-61906d89d479"
default: "ost://horizon.cloud.cnaf.infn.it/3d993ab8-5d7b-4362-8fd6-af1391edca39"
#default: "ost://cloud.recas.ba.infn.it/1113d7e8-fc5d-43b9-8d26-61906d89d479"
helm_cod_values:
type: string
Expand All @@ -68,18 +80,35 @@ topology_template:
ips:
- {{ .ExternalIP }}
gsi:
enabled: true
enabled: {{ .GsiEnabled }}
{{- if .GsiEnabled }}
vo: {{ .VO }}
vomses:
- filename: {{ .VomsFile.Name }}
content: | {{ .VomsFile.Content | indent 14 }}
caCert:
cert: | {{ .CacheCert | indent 14 }}
key: | {{ .CacheKey | indent 14 }}
proxy: true
{{- end }}
cache:
redirHost: {{ .Redirector }}
{{- if .RedirectorHost }}
redirHost: {{ .RedirectorHost }}
{{- end }}
originHost: {{ .OriginHost }}
originXrdPort: {{ .OriginPort }}
redirector:
{{- if .RedirectorHost }}
enabled: false
{{- end}}
service:
cms:
port: {{ .RedirectorPort }}
proxy:
{{- if .RedirectorHost }}
enabled: false
{{- end}}
node_templates:
Expand Down Expand Up @@ -163,5 +192,4 @@ topology_template:
outputs:
k8s_endpoint:
value: { concat: [ 'https://', get_attribute: [ k3s_master_server, public_address, 0 ], ':30443' ] }
`
13 changes: 8 additions & 5 deletions cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"github.com/spf13/cobra"
)

var validate bool

// createCmd represents the create command
var createCmd = &cobra.Command{
Use: "create <Template1> ... <TemplateN>",
Expand All @@ -37,11 +39,12 @@ var createCmd = &cobra.Command{
panic(err)
}

err = clientConf.Validate(template)
if err != nil {
panic(err)
if validate {
err = clientConf.Validate(template)
if err != nil {
panic(err)
}
}

_, err = clientConf.CreateInf(template)
if err != nil {
panic(err)
Expand All @@ -55,7 +58,7 @@ func init() {

rootCmd.AddCommand(createCmd)
// Here you will define your flags and configuration settings.

createCmd.PersistentFlags().BoolVar(&validate, "validate", true, "Validate before submitting")
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// Cobra also supports local flags, which will only run
Expand Down
4 changes: 2 additions & 2 deletions docs/dodas.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ dodas [flags]

### SEE ALSO

* [dodas app](dodas_app.md) - Install Kubernetes apps from helm charts or YAML files
* [dodas app](dodas_app.md) - Install Kubernetes cluster and apps from helm charts or YAML files
* [dodas autocomplete](dodas_autocomplete.md) - Generate script for bash autocomplete
* [dodas create](dodas_create.md) - Create a cluster from a TOSCA template
* [dodas destroy](dodas_destroy.md) - Destroy infrastructure with this InfID
Expand All @@ -47,4 +47,4 @@ dodas [flags]
* [dodas version](dodas_version.md) - Client version
* [dodas zsh-autocomplete](dodas_zsh-autocomplete.md) - Generate script for zsh autocomplete

###### Auto generated by spf13/cobra on 7-Feb-2020
###### Auto generated by spf13/cobra on 17-Feb-2020
Loading

0 comments on commit 7b216f4

Please sign in to comment.