Skip to content

Commit

Permalink
Change functions inputs to match the new metadata reading way
Browse files Browse the repository at this point in the history
  • Loading branch information
asaif committed Oct 13, 2016
1 parent 58aae0a commit 58ee239
Showing 1 changed file with 26 additions and 36 deletions.
62 changes: 26 additions & 36 deletions libinizer/inize.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,31 @@ import (
"log"
"os"
"os/exec"
reg "regexp"
"strings"
"syscall"
)

//Get Metadata endpoints
var get = initClient()
//Get Metadata
var get = GetJson()

func AssignAnchorIP() {
if ok, _ := get.FloatingIPv4Active(); ok {
ip, err := get.AnchorIPv4()
if err != nil {
log.Println(err)
if ok := get.FloatingIP.IPv4.Active; ok {
ip := get.Interfaces["public"][0].AnchorIPv4.IPAddress
if ip == "" {
log.Println("No anchor IP assigned")
} else {
// On the Fly
Rcmd("/sbin/ip", "addr", "add", ip, "dev", "eth0")
}
}

}

// Check ROOT disk size
func CurrentDiskSize() int64 {
fs := syscall.Statfs_t{}
err := syscall.Statfs("/", &fs)
if err != nil {
fmt.Println(err)
log.Println(err)
}
size := fs.Blocks * uint64(fs.Bsize)
return int64(size / 1024 / 1024 / 1024)
Expand All @@ -45,43 +44,29 @@ func CurrentDiskSize() int64 {
func DecypherRootPass() string {
var key = []byte{118, 12, 93, 186, 49, 127, 222, 88, 160, 100, 184, 98, 50, 179, 192, 14}
var iv = []byte{77, 40, 28, 251, 23, 94, 236, 11, 124, 233, 83, 207, 161, 61, 68, 68}
crtpdtxt, _ := get.RootPass()
Password, _ := base64.StdEncoding.DecodeString(string(crtpdtxt))
crtpdtxt := get.RootPass
password, _ := base64.StdEncoding.DecodeString(string(crtpdtxt))
block, _ := aes.NewCipher(key)
if len(Password) < aes.BlockSize {
if len(password) < aes.BlockSize {
log.Fatal("cipher text is too short")
return ""
}
stream := cipher.NewCFBDecrypter(block, iv)
stream.XORKeyStream(Password, Password)
// This section to validate we don't have the ugly Erlang characters (^D^@^@^@)
nonNumricpass := string(Password)
var re = reg.MustCompile(`^[^[:alpha:]]*`)
password := re.ReplaceAllLiteralString(nonNumricpass, ``)
return password
}

// Get disk size
func ReceviedDiskSize() int64 {
size, _ := get.DiskSize()
return size
stream.XORKeyStream(password, password)
p := password[0:1][0]
return string(password[p:])
}

// Fetch Vendor Data
func GetVendorData() (string, error) {
action, err := get.VendorData()
if err != nil {
return "", err
action := get.VendorData
if action == "" {
log.Println("Cannot fetch vendor data")
return "", fmt.Errorf("Cannot fetch vendor data")
}
return action, nil
}

// Initiate Metadata Client
func initClient() *Client {
opts := WithBaseURL(defaultBaseURL)
return NewClient(opts)
}

// Initialize the VM for the first time
func InizeVm() error {
err := SetRootPassword()
Expand All @@ -107,6 +92,12 @@ func Rcmd(command string, args ...string) (otp bytes.Buffer, err error) {
}
}

// Get disk size
func ReceviedDiskSize() int64 {
size := get.DiskSize
return size
}

// Resize ROOT disk based on fetched info
func ResizeDisk() {
if CurrentDiskSize() < ReceviedDiskSize() {
Expand All @@ -117,7 +108,7 @@ func ResizeDisk() {

// Execute user data that was provided by the user during VM creation.
func RunUserData() {
UserData, _ := get.UserData()
UserData := get.UserData
if len(UserData) > 0 {
if ok := ValidScript(UserData); ok {
ioutil.WriteFile("/tmp/"+"usrscript", []byte(UserData), 0700)
Expand All @@ -126,7 +117,6 @@ func RunUserData() {
}
}

// Set root password provided By the API upon VM creation.
func SetRootPassword() error {
password := DecypherRootPass()
cmd := exec.Command("/usr/sbin/chpasswd")
Expand Down Expand Up @@ -160,7 +150,7 @@ func ValidScript(UserData string) bool {
// Write the public SSH key(s) to authorized_keys file which were added to
// the VM during its creation.
func WritePublicKeys() {
keys, _ := get.PublicKeys()
keys := get.PublicKeys
// Create .ssh directory if not exist
SshPath := "/root/.ssh/"
if _, err := os.Stat(SshPath); os.IsNotExist(err) {
Expand Down

0 comments on commit 58ee239

Please sign in to comment.