Skip to content

Commit ea36977

Browse files
committed
added strategy to download latest agent/plugin or xcompile
1 parent 3bdfebe commit ea36977

File tree

15 files changed

+296
-51
lines changed

15 files changed

+296
-51
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
kiss
44
tmp
55
.goxc.local.json
6+
debian/

.kiss.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
# agent:
2+
# path: "./agent"
3+
# force: true
4+
5+
# plugins:
6+
# apt:
7+
# path: "./plugins/plugin-apt"
8+
# force: true
9+
# shell:
10+
# path: "./plugins/plugin-shell"
11+
# force: true
12+
113
# Global vars
214
vars:
315
application: go-example-app

Vagrantfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
1010
# please see the online documentation at vagrantup.com.
1111

1212
# Every Vagrant virtual environment requires a box to build off of.
13-
config.vm.box = "hashicorp/precise64"
13+
config.vm.box = "ubuntu/trusty64"
1414

1515
# Disable automatic box update checking. If you disable this, then
1616
# boxes will only be checked for updates when the user runs

bintray/bintray.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package bintray
2+
3+
var (
4+
apiEndpoint = "https://api.bintray.com"
5+
)

bintray/connection.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package bintray
2+
3+
import (
4+
"fmt"
5+
"io/ioutil"
6+
"net/http"
7+
"os"
8+
"strings"
9+
)
10+
11+
func get(path string) []byte {
12+
client := &http.Client{}
13+
14+
paths := []string{apiEndpoint, path}
15+
16+
req, err := http.NewRequest("GET", strings.Join(paths, "/"), nil)
17+
if err != nil {
18+
fmt.Printf("%s", err)
19+
os.Exit(1)
20+
}
21+
22+
response, err := client.Do(req)
23+
if err != nil {
24+
fmt.Printf("%s", err)
25+
os.Exit(1)
26+
}
27+
28+
defer response.Body.Close()
29+
contents, err := ioutil.ReadAll(response.Body)
30+
if err != nil {
31+
fmt.Printf("%s", err)
32+
os.Exit(1)
33+
}
34+
35+
return contents
36+
}

bintray/packages.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package bintray
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
)
7+
8+
type Package struct {
9+
Name string
10+
Repo string
11+
Owner string
12+
LatestVersion string `json:"latest_version"`
13+
}
14+
15+
func GetPackage(subject string, repo string, pkg string) (*Package, error) {
16+
path := fmt.Sprintf("/packages/%v/%v/%v", subject, repo, pkg)
17+
bytes := get(path)
18+
19+
var result Package
20+
err := json.Unmarshal(bytes, &result)
21+
if err != nil {
22+
return nil, err
23+
}
24+
25+
return &result, nil
26+
}

commands/run-role.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func runRoleRun(cmd *cobra.Command, args []string) {
5353

5454
logger.Infof("Running role `%v`", roleName)
5555
for _, host := range config.Hosts.WithRole(roleName) {
56-
runWithRunner(role.Tasks, &host)
56+
runWithRunner(role.Tasks, &host, config)
5757
}
5858

5959
logger.Info("Tasks completed successfully")

commands/run.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,14 @@ func runRun(cmd *cobra.Command, args []string) {
6060
log.Fatalf("Host not found: %v\n", hostName)
6161
}
6262

63-
runWithRunner(configuration.NewTaskCollection(*task), host)
63+
runWithRunner(configuration.NewTaskCollection(*task), host, config)
6464
logger.Info("Tasks completed successfully")
6565
os.Exit(0)
6666
} else {
6767
for _, host := range config.Hosts {
6868
task := host.Tasks.Get(taskName)
6969
if task != nil {
70-
runWithRunner(configuration.NewTaskCollection(*task), &host)
70+
runWithRunner(configuration.NewTaskCollection(*task), &host, config)
7171
}
7272
}
7373

@@ -79,11 +79,11 @@ func runRun(cmd *cobra.Command, args []string) {
7979
os.Exit(1)
8080
}
8181

82-
func runWithRunner(tasks configuration.TaskCollection, host *configuration.Host) bool {
82+
func runWithRunner(tasks configuration.TaskCollection, host *configuration.Host, config *configuration.Configuration) bool {
8383
logger.Infof("Selecting host `%v`", host.Host)
8484

85-
runner := core.NewRemoteRunner(host)
86-
if err := runner.BeforeAll(); err != nil {
85+
runner := core.NewRemoteRunner(host, config)
86+
if err := runner.BeforeAll(tasks); err != nil {
8787
logger.Fatal(err.Error())
8888
os.Exit(1)
8989
}

configuration/config.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,25 @@ import (
99

1010
var logger = logging.GetLogger("kiss")
1111

12+
type Agent struct {
13+
Path string
14+
Force bool
15+
}
16+
17+
type PluginConfig struct {
18+
Path string
19+
Force bool
20+
}
21+
1222
// Configuration holds information about
1323
// variables, hosts and tasks.
1424
type Configuration struct {
15-
Vars map[string]string
16-
Hosts HostCollection
17-
Tasks TaskCollection
18-
Roles RoleCollection
25+
Vars map[string]string
26+
Hosts HostCollection
27+
Tasks TaskCollection
28+
Roles RoleCollection
29+
Agent Agent
30+
Plugins map[string]PluginConfig
1931
}
2032

2133
// Load loads configuration from the given path.

configuration/tasks.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,28 @@ func (t TaskCollection) Get(name string) *Task {
3737
return nil
3838
}
3939

40+
func (tasks TaskCollection) UniquePluginNames() []string {
41+
var plugins []string
42+
43+
for _, t := range tasks {
44+
for key, _ := range t.Plugin {
45+
found := false
46+
for _, a := range plugins {
47+
if a == key {
48+
found = true
49+
break
50+
}
51+
}
52+
53+
if !found {
54+
plugins = append(plugins, key)
55+
}
56+
}
57+
}
58+
59+
return plugins
60+
}
61+
4062
func NewTaskCollection(tasks ...Task) TaskCollection {
4163
var result TaskCollection
4264

core/bintray.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package core
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/tobscher/kiss/bintray"
7+
)
8+
9+
var (
10+
subject = "tobscher"
11+
repo = "generic"
12+
prefix = "kiss"
13+
)
14+
15+
func bintrayDownloadUrl(pkg string, os string, arch string) string {
16+
p := fmt.Sprintf("%v-%v", prefix, pkg)
17+
bintrayPackage, err := bintray.GetPackage(subject, repo, p)
18+
if err != nil {
19+
return ""
20+
}
21+
22+
version := bintrayPackage.LatestVersion
23+
ext := ".tar.gz"
24+
25+
path := fmt.Sprintf("https://dl.bintray.com/%v/%v/%v_%v_%v_%v%v", subject, repo, pkg, version, os, arch, ext)
26+
27+
return path
28+
}

core/compiler.go

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
package core
22

33
import (
4+
"encoding/json"
45
"fmt"
6+
"io/ioutil"
7+
"os"
58
"os/exec"
69
"path"
10+
"strings"
711
)
812

913
var (
10-
compiler = "goxc"
14+
compiler = "goxc"
15+
defaultVersion = "snapshot"
1116
)
1217

1318
func compileDirectory(directory string, targetOs string, targetArch string) (*string, error) {
@@ -34,7 +39,34 @@ func compileDirectory(directory string, targetOs string, targetArch string) (*st
3439
return nil, err
3540
}
3641

42+
version := goxcVersion(directory)
43+
3744
directoryName := path.Base(directory)
38-
file := fmt.Sprintf("%v/tmp/snapshot/%v_%v/%v", directory, targetOs, targetArch, directoryName)
45+
file := fmt.Sprintf("%v/tmp/%v/%v_%v/%v", directory, version, targetOs, targetArch, directoryName)
3946
return &file, nil
4047
}
48+
49+
func goxcVersion(directory string) string {
50+
filePath := strings.Join([]string{directory, ".goxc.json"}, "/")
51+
file, err := os.Open(filePath)
52+
if err != nil {
53+
return defaultVersion
54+
}
55+
defer file.Close()
56+
57+
bytes, err := ioutil.ReadAll(file)
58+
if err != nil {
59+
return defaultVersion
60+
}
61+
62+
var config struct {
63+
PackageVersion string
64+
}
65+
66+
err = json.Unmarshal(bytes, &config)
67+
if err != nil {
68+
return defaultVersion
69+
}
70+
71+
return config.PackageVersion
72+
}

0 commit comments

Comments
 (0)