Skip to content

Commit

Permalink
Merge pull request #148 from Appliscale/firstrun
Browse files Browse the repository at this point in the history
User Experience improvement - First run #133
  • Loading branch information
maxiwoj authored Sep 3, 2018
2 parents fc195e8 + a8725bf commit 12ec5e7
Show file tree
Hide file tree
Showing 12 changed files with 538 additions and 75 deletions.
Binary file added .Makefile.swp
Binary file not shown.
5 changes: 4 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ env:
global:
- secure: "roiMALXQRkKKlPP+cbtac795XeHHnPrKvc6M8HjVa5gzL1SZXHDn/zvcvTc//AAMBFxZXb9+V+USrU3vNyzULIoh5j7nTOutPtik4WYLIfwADVgSxpiEG9jDBYp3DLlXMAMb5uJWRuG4DSSxpUGPFujQr9+aaUa0Lz6tsPUpE2/yaQfR5GmrAVteevkEZ0h18BxiRKsf4cRIGEQpwOSAQ+x66MlaEAn/kkprS4K9imniHeDRnz4TM75SAI7VZh9Fglv6gf8HTZ73EV1sGzsbG8+k1eO53UxMYR7YneB86ihKjRFuXSXzT8K0RdoACY6Muf81kwP5JUP3SLMaNgRTrdGmzOZ+yhJzfuey0b7i2llvGHrfKCmEIUzTYC5KZgvwblqwU97u55baJpAwQJgwPOKBO2RUvyzdK00t39kj+PpGrmyrW9evQ7mGk7o7txn4K1LJfCTn/fll1Y3B1poyiflYfQ4vlj7RgKSwAgIs36+yPhYF/m1K9I+jy8BRHPCZek3JJIMnKaW3OEBzpCRBoxxmMFomN8tq2Ygde5TUVxukAbZNIrlaPPWB5ZB6ZQ1hKol0yddkqOKxivGPQJaZthlt7eHpx4rAG+ZY1e706pG3iqYcTEVJTjPPzCaKdSpbFg3oKGet3ClimF0ycNpJRFQs2eveI3AkhVCN2Z4IWX8=" #Github

install: make config-install get-deps
install:
- 'mkdir -p $HOME/.config/perun'
- 'cp defaults/main.yaml $HOME/.config/perun/main.yaml'
- make get-deps

script:
- make code-analysis
Expand Down
10 changes: 1 addition & 9 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
.PHONY: config-install get-deps code-analysis test all
.PHONY: get-deps code-analysis test all

all: get-deps code-analysis test

config-install:
mkdir -p "$(HOME)/.config/perun/stack-policies"
cp defaults/main.yaml "$(HOME)/.config/perun/main.yaml"
cp defaults/style.yaml "$(HOME)/.config/perun/style.yaml"
cp defaults/specification_inconsistency.yaml "$(HOME)/.config/perun/specification_inconsistency.yaml"
cp defaults/blocked.json "$(HOME)/.config/perun/stack-policies/blocked.json"
cp defaults/unblocked.json "$(HOME)/.config/perun/stack-policies/unblocked.json"

get-deps:
go get -t -v ./...
go install ./...
Expand Down
275 changes: 275 additions & 0 deletions checkingrequiredfiles/checkingrequiredfiles.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,275 @@
package checkingrequiredfiles

import (
"bufio"
"github.com/Appliscale/perun/configurator"
"github.com/Appliscale/perun/context"
"github.com/Appliscale/perun/helpers"
"github.com/Appliscale/perun/logger"
"github.com/Appliscale/perun/myuser"
"github.com/go-ini/ini"
"io"
"net/http"
"os"
"strings"
)

//CheckingRequiredFiles looks for required and default files and if doesn't find will create these.
func CheckingRequiredFiles(ctx *context.Context) {
myLogger := logger.CreateDefaultLogger()

mainYAMLexists, mainError := isMainYAMLPresent(&myLogger)
if mainError != nil {
myLogger.Error(mainError.Error())
}

configAWSExists, configError := isAWSConfigPresent(&myLogger)
if configError != nil {
myLogger.Error(configError.Error())
}

credentialsExists, credentialsError := isCredentialsPresent(&myLogger)
if credentialsError != nil {
myLogger.Error(credentialsError.Error())
}

homePath, pathError := myuser.GetUserHomeDir()
if pathError != nil {
myLogger.Error(pathError.Error())
}

profile := "default"
region := "us-east-1"

if !mainYAMLexists {
if configAWSExists {
profile, *ctx = configIsPresent(profile, homePath, ctx, myLogger)
if !credentialsExists {
createCredentials(profile, homePath, ctx, &myLogger)
}

} else { //configAWSExists == false
var answer string
myLogger.GetInput("Config doesn't exist, create default *Y* or new *N*?", &answer)
if strings.ToUpper(answer) == "N" {
profile, region, *ctx = newConfigFile(profile, region, homePath, ctx, &myLogger)
addProfileToCredentials(profile, homePath, ctx, &myLogger)
addNewProfileFromCredentialsToConfig(profile, homePath, ctx, &myLogger)

} else if strings.ToUpper(answer) == "Y" {
configurator.CreateAWSConfigFile(ctx, profile, region)
*ctx = createNewMainYaml(profile, homePath, ctx, &myLogger)
configurator.CreateAWSCredentialsFile(ctx, profile)
}

if credentialsExists {
createCredentials(profile, homePath, ctx, &myLogger)
}
}
} else { //mainYAMLexists == true
if configAWSExists {
if !credentialsExists {
myLogger.Always("Profile from main.yaml: " + ctx.Config.DefaultProfile)
configurator.CreateAWSCredentialsFile(ctx, ctx.Config.DefaultProfile)
} else {
isProfileInPresent := isProfileInCredentials(ctx.Config.DefaultProfile, homePath+"/.aws/credentials", &myLogger)
if !isProfileInPresent {
myLogger.Always("Profile from main.yaml: " + ctx.Config.DefaultProfile)
configurator.CreateAWSCredentialsFile(ctx, ctx.Config.DefaultProfile)
}
}
} else { //configAWSExists ==false
var answer string
myLogger.GetInput("Config doesn't exist, create default - "+ctx.Config.DefaultProfile+" *Y* or new *N*?", &answer)
if strings.ToUpper(answer) == "Y" {
configurator.CreateAWSConfigFile(ctx, ctx.Config.DefaultProfile, ctx.Config.DefaultRegion)
} else if strings.ToUpper(answer) == "N" {
profile, region, *ctx = newConfigFile(profile, region, homePath, ctx, &myLogger)
addProfileToCredentials(profile, homePath, ctx, &myLogger)
}
addNewProfileFromCredentialsToConfig(ctx.Config.DefaultProfile, homePath, ctx, &myLogger)

if credentialsExists {
createCredentials(ctx.Config.DefaultProfile, homePath, ctx, &myLogger)

}
}
}
downloadError := downloadDefaultFiles()
if downloadError != nil {
myLogger.Error(downloadError.Error())
}
}

// Looking for main.yaml.
func isMainYAMLPresent(myLogger *logger.Logger) (bool, error) {
homePath, pathError := myuser.GetUserHomeDir()
if pathError != nil {
myLogger.Error(pathError.Error())
return false, pathError
}
_, mainError := os.Open(homePath + "/.config/perun/main.yaml")
if mainError != nil {
_, mainError = os.Open(homePath + "/etc/perun/main.yaml")
if mainError != nil {
return false, pathError
}
return true, pathError
}
return true, pathError
}

// Looking for .aws.config.
func isAWSConfigPresent(myLogger *logger.Logger) (bool, error) {
homePath, pathError := myuser.GetUserHomeDir()
if pathError != nil {
myLogger.Error(pathError.Error())
return false, pathError
}
_, credentialsError := os.Open(homePath + "/.aws/config")
if credentialsError != nil {
return false, credentialsError
}
return true, nil

}

// Looking for .aws/credentials.
func isCredentialsPresent(myLogger *logger.Logger) (bool, error) {
homePath, pathError := myuser.GetUserHomeDir()
if pathError != nil {
myLogger.Error(pathError.Error())
return false, pathError
}
_, credentialsError := os.Open(homePath + "/.aws/credentials")
if credentialsError != nil {
return false, credentialsError
}
return true, nil
}

// Looking for [profiles] in credentials or config and return all.
func getProfilesFromFile(path string, mylogger *logger.Logger) []string {
credentials, credentialsError := os.Open(path)
if credentialsError != nil {
mylogger.Error(credentialsError.Error())
return []string{}
}
defer credentials.Close()
profiles := make([]string, 0)
scanner := bufio.NewScanner(credentials)
for scanner.Scan() {
if strings.Contains(scanner.Text(), "[") {
profile := strings.TrimPrefix(scanner.Text(), "[")
profile = strings.TrimSuffix(profile, "]")
if strings.Contains(profile, "profile ") {
profile = strings.TrimPrefix(profile, "profile ")
}
if strings.Contains(profile, "-long-term") {
profile = strings.TrimSuffix(profile, "-long-term")
}
profiles = append(profiles, profile)
}
}
return profiles
}

// Looking for user's profile in credentials or config.
func isProfileInCredentials(profile string, path string, mylogger *logger.Logger) bool {
credentials, credentialsError := os.Open(path)
if credentialsError != nil {
mylogger.Error(credentialsError.Error())
}
defer credentials.Close()
scanner := bufio.NewScanner(credentials)
for scanner.Scan() {
if strings.Contains(scanner.Text(), "["+profile+"]") || strings.Contains(scanner.Text(), "["+profile+"-long-term]") {
return true
}
}
return false

}

// Looking for region for profile.
func findRegionForProfile(profile string, path string, mylogger *logger.Logger) string {
configuration, loadError := ini.Load(path)
if loadError != nil {
mylogger.Error(loadError.Error())
}
section, sectionError := configuration.GetSection(profile)
if sectionError != nil {
section, sectionError = configuration.GetSection("profile " + profile)
if sectionError != nil {
mylogger.Error(sectionError.Error())
return ""
}
}
region := section.Key("region").Value()
return region

}

// Getting profiles from credentials and config, if credentials has new profile, add to config.
func findNewProfileInCredentials(credentials []string, config []string) []string {
profiles := make([]string, 0)
for i, cred := range credentials {
if strings.Contains(cred, "-long-term") {
cred = strings.TrimSuffix(cred, "-long-term")
credentials[i] = cred
}
}
for _, cred := range credentials {
isProfileHere := helpers.SliceContains(config, cred)
if !isProfileHere {
profiles = append(profiles, cred)
return profiles
}
}
return []string{}
}

// Downloading other files.
func downloadDefaultFiles() error {
urls := make(map[string]string)
urls["blocked.json"] = "https://s3.amazonaws.com/perun-default-file/blocked.json"
urls["unblocked.json"] = "https://s3.amazonaws.com/perun-default-file/unblocked.json"
urls["style.yaml"] = "https://s3.amazonaws.com/perun-default-file/style.yaml"
urls["specification_inconsistency.yaml"] = "https://s3.amazonaws.com/perun-default-file/specification_inconsistency.yaml"

for file, url := range urls {
homePath, _ := myuser.GetUserHomeDir()
homePath += "/.config/perun/"

if strings.Contains(file, "blocked") {
homePath += "stack-policies/"
}

_, err := os.Stat(homePath)
if os.IsNotExist(err) {
os.Mkdir(homePath, 0755)
}

_, openError := os.Open(homePath + file) //checking if file exists
if openError != nil {
out, creatingFileError := os.Create(homePath + file)

if creatingFileError != nil {
return creatingFileError
}
defer out.Close()

resp, httpGetError := http.Get(url)
if httpGetError != nil {
return httpGetError
}
defer resp.Body.Close()

_, copyError := io.Copy(out, resp.Body)
if copyError != nil {
return copyError
}
}
}
return nil
}
Loading

0 comments on commit 12ec5e7

Please sign in to comment.