Created
April 29, 2015 14:18
-
-
Save aweiteka/cfdef8cd4bddcb6a907b to your computer and use it in GitHub Desktop.
Convert nulecule application to openshift template
This file contains hidden or 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
| package main | |
| import "fmt" | |
| import "encoding/json" | |
| import "os" | |
| import "io/ioutil" | |
| import "path/filepath" | |
| import "github.com/ghodss/yaml" | |
| const openshiftApiVer = "v1beta1" | |
| const openshiftKind = "Template" | |
| const atomicAppTemplateFile = "nulecule" | |
| const atomicAppProviderName = "kubernetes" | |
| // helper function to check errors | |
| func check(e error) { | |
| if e != nil { | |
| panic(e) | |
| } | |
| } | |
| type OpenshiftTemplate struct { | |
| ApiVersion string `json:"apiVersion"` | |
| Kind string `json:"kind"` | |
| OItems map[string]interface{} `json:"items"` | |
| OMetadata `json:"metadata"` | |
| OParameters `json:"parameters"` | |
| OLabels []string `json:"labels"` | |
| } | |
| type OMetadata struct { | |
| Name string `json:"name"` | |
| Annotations map[string]interface{} `json:"annotations"` | |
| } | |
| type OParameters struct { | |
| Parameters []string `json:"parameters"` | |
| } | |
| type AtomicTemplate struct { | |
| Id string `json:"id"` | |
| Specversion string `json:"specversion"` | |
| AMetadata map[string]interface{} `json:"metadata"` | |
| AGraph `json:"graph"` | |
| ARequirements `json:"requirements"` | |
| } | |
| type AGraph struct { | |
| AItems []string | |
| } | |
| type AItems struct { | |
| ASource string `json:"source"` | |
| AParams `json:"params"` | |
| AArtifacts map[string]interface{} `json:"artifacts"` | |
| } | |
| type AParams struct { | |
| ADescription string `json:"description"` | |
| AConstraints `json:"constraints"` | |
| ADefault string `json:"default"` | |
| } | |
| type AConstraints struct { | |
| AAllowedPattern string `json:"allowed_pattern"` | |
| ADescription string `json:"description"` | |
| } | |
| type ARequirements struct { | |
| AName string `json:"name"` | |
| AAccessModes string `json:"accessModes"` | |
| ASize int `json:"size"` | |
| } | |
| func getFile(file string) []byte { | |
| contents, err := ioutil.ReadFile(file) | |
| check(err) | |
| return contents | |
| } | |
| func getContext(args []string) string { | |
| if len(args) > 2 { | |
| fmt.Printf("Usage : %s [path/to/Atomicapp]\n ", args[0]) | |
| os.Exit(1) | |
| } else if len(args) == 1 { | |
| // current working dir not quite right | |
| dir, err := filepath.Abs(filepath.Dir(os.Args[0])) | |
| check(err) | |
| return dir | |
| } else if !IsDirectory(args[1]) { | |
| fmt.Printf("'%s' must be a directory\n ", args[1]) | |
| os.Exit(1) | |
| } | |
| return string(args[1]) | |
| } | |
| func IsDirectory(path string) bool { | |
| fileInfo, err := os.Stat(path) | |
| check(err) | |
| return fileInfo.IsDir() | |
| } | |
| func main() { | |
| context := getContext(os.Args) | |
| jsonfile := getFile("guestbook-controller.json") | |
| atomicappfile := context + "/" + atomicAppTemplateFile | |
| //fmt.Println(atomicappfile) | |
| atomicjson, err := yaml.YAMLToJSON(getFile(atomicappfile)) | |
| check(err) | |
| var odat map[string]interface{} | |
| var adat map[string]interface{} | |
| json.Unmarshal(atomicjson, &adat) | |
| json.Unmarshal(jsonfile, &odat) | |
| metadata := adat["metadata"].(map[string]interface{}) | |
| /* | |
| atemplate := &AtomicTemplate{ | |
| Id: "", | |
| Specversion: "", | |
| AMetadata: AMetadata, | |
| AGraph: AGraph, | |
| ARequirements: ARequirements} | |
| */ | |
| otemplate := &OpenshiftTemplate{ | |
| ApiVersion: openshiftApiVer, | |
| Kind: openshiftKind, | |
| OMetadata: OMetadata{ | |
| Name: metadata["name"].(string), | |
| Annotations: metadata}, | |
| OItems: odat} | |
| enc := json.NewEncoder(os.Stdout) | |
| enc.Encode(otemplate) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment