github.com/coreos/mantle@v0.13.0/cmd/ore/packet/create-device.go (about) 1 // Copyright 2017 CoreOS, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package packet 16 17 import ( 18 "encoding/json" 19 "fmt" 20 "io/ioutil" 21 "os" 22 23 "github.com/spf13/cobra" 24 25 ctplatform "github.com/coreos/container-linux-config-transpiler/config/platform" 26 "github.com/coreos/mantle/platform/conf" 27 ) 28 29 var ( 30 cmdCreateDevice = &cobra.Command{ 31 Use: "create-device [options]", 32 Short: "Create Packet device", 33 Long: `Create a Packet device.`, 34 RunE: runCreateDevice, 35 } 36 hostname string 37 userDataPath string 38 ) 39 40 func init() { 41 Packet.AddCommand(cmdCreateDevice) 42 cmdCreateDevice.Flags().StringVar(&options.Facility, "facility", "sjc1", "facility code") 43 cmdCreateDevice.Flags().StringVar(&options.Plan, "plan", "", "plan slug (default board-dependent, e.g. \"baremetal_0\")") 44 cmdCreateDevice.Flags().StringVar(&options.Board, "board", "amd64-usr", "Container Linux board") 45 cmdCreateDevice.Flags().StringVar(&options.InstallerImageBaseURL, "installer-image-base-url", "", "installer image base URL, non-https (default board-dependent, e.g. \"http://stable.release.core-os.net/amd64-usr/current\")") 46 cmdCreateDevice.Flags().StringVar(&options.ImageURL, "image-url", "", "image base URL (default board-dependent, e.g. \"https://alpha.release.core-os.net/amd64-usr/current/coreos_production_packet_image.bin.bz2\")") 47 cmdCreateDevice.Flags().StringVar(&hostname, "hostname", "", "hostname to assign to device") 48 cmdCreateDevice.Flags().StringVar(&userDataPath, "userdata-file", "", "path to file containing userdata") 49 } 50 51 func runCreateDevice(cmd *cobra.Command, args []string) error { 52 if len(args) != 0 { 53 fmt.Fprintf(os.Stderr, "Unrecognized args in packet create-device cmd: %v\n", args) 54 os.Exit(2) 55 } 56 57 userdata := conf.Empty() 58 if userDataPath != "" { 59 data, err := ioutil.ReadFile(userDataPath) 60 if err != nil { 61 fmt.Fprintf(os.Stderr, "Couldn't read userdata file %v: %v\n", userDataPath, err) 62 os.Exit(1) 63 } 64 userdata = conf.Unknown(string(data)) 65 } 66 conf, err := userdata.Render(ctplatform.Packet) 67 if err != nil { 68 fmt.Fprintf(os.Stderr, "Couldn't parse userdata file %v: %v\n", userDataPath, err) 69 os.Exit(1) 70 } 71 72 device, err := API.CreateDevice(hostname, conf, nil) 73 if err != nil { 74 fmt.Fprintf(os.Stderr, "Couldn't create device: %v\n", err) 75 os.Exit(1) 76 } 77 78 err = json.NewEncoder(os.Stdout).Encode(&struct { 79 ID string `json:"id"` 80 Hostname string `json:"hostname"` 81 IP string `json:"public-ip,omitempty"` 82 }{ 83 ID: device.ID, 84 Hostname: device.Hostname, 85 IP: API.GetDeviceAddress(device, 4, true), 86 }) 87 if err != nil { 88 fmt.Fprintf(os.Stderr, "Couldn't encode result: %v\n", err) 89 os.Exit(1) 90 } 91 return nil 92 }