factor 0.0.6 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/cli/channel_task.rb +28 -11
- data/lib/cli/command.rb +10 -0
- data/lib/cli/credential_task.rb +11 -5
- data/lib/cli/factor_task.rb +7 -27
- data/lib/cli/server_task.rb +4 -7
- data/lib/cli/workflow_task.rb +20 -2
- data/lib/client/client.rb +88 -6
- data/lib/runtime/engine.rb +28 -12
- metadata +49 -1
data/lib/cli/channel_task.rb
CHANGED
@@ -1,25 +1,42 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
require 'cli/command'
|
3
|
+
require 'zip/zip'
|
4
|
+
require 'zip/zipfilesystem'
|
3
5
|
|
4
6
|
module Factor
|
5
7
|
module CLI
|
6
8
|
class ChannelTask < Command
|
7
|
-
|
8
|
-
desc "
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
9
|
+
|
10
|
+
desc "call CHANNEL METHOD TARGET","start a workflow"
|
11
|
+
method_option :parameters, :type=>:hash, :default=>{}, :required=>false
|
12
|
+
def call(channel, method, target)
|
13
|
+
|
14
|
+
puts "not implemented"
|
15
|
+
|
14
16
|
end
|
15
17
|
|
16
|
-
|
18
|
+
|
19
|
+
|
20
|
+
desc "list", "list all the channels"
|
21
|
+
#method_option :key, :alias=>"-k", :type=>:string, :desc=>"key reference"
|
17
22
|
def list
|
23
|
+
puts @client.get_channels
|
18
24
|
end
|
19
|
-
|
20
|
-
desc "
|
21
|
-
def
|
25
|
+
|
26
|
+
desc "add NAME DIRECTORY", "add a key and value for the credential"
|
27
|
+
def add(name,directory)
|
28
|
+
|
29
|
+
#contents=File.open(File.expand_path(filename), "rb") {|f| f.read}
|
30
|
+
|
31
|
+
puts @client.add_channel(name,directory)
|
32
|
+
end
|
33
|
+
|
34
|
+
desc "remove NAME", "remove a workflow"
|
35
|
+
def remove(name)
|
36
|
+
puts @client.remove_channel(name)
|
22
37
|
end
|
38
|
+
|
39
|
+
|
23
40
|
end
|
24
41
|
end
|
25
42
|
end
|
data/lib/cli/command.rb
CHANGED
@@ -9,11 +9,21 @@ module Factor
|
|
9
9
|
module CLI
|
10
10
|
class Command < Thor
|
11
11
|
CONFIG_FILE_DIR = File.expand_path("~/.factor")
|
12
|
+
|
13
|
+
|
12
14
|
no_tasks do
|
15
|
+
|
16
|
+
def initialize(*vals)
|
17
|
+
@client = Factor::Client::Client.new
|
18
|
+
@client.login_token(get_config[:token])
|
19
|
+
super(*vals)
|
20
|
+
end
|
21
|
+
|
13
22
|
def save_config(config)
|
14
23
|
File.open(CONFIG_FILE_DIR,'w') do |file|
|
15
24
|
YAML::dump(config,file)
|
16
25
|
end
|
26
|
+
@client.login_token(get_config[:token])
|
17
27
|
end
|
18
28
|
|
19
29
|
def get_config
|
data/lib/cli/credential_task.rb
CHANGED
@@ -5,16 +5,22 @@ module Factor
|
|
5
5
|
module CLI
|
6
6
|
class CredentialTask < Command
|
7
7
|
|
8
|
-
desc "list", "list all the credentials"
|
9
|
-
|
8
|
+
desc "list [KEY]", "list all the credentials"
|
9
|
+
#method_option :key, :alias=>"-k", :type=>:string, :desc=>"key reference"
|
10
|
+
def list(key="")
|
11
|
+
puts @client.get_credential(key)["value"]
|
10
12
|
end
|
11
13
|
|
12
|
-
desc "
|
13
|
-
|
14
|
+
desc "set KEY VALUE", "add a key and value for the credential"
|
15
|
+
#method_option :key, :alias=>"-k", :type=>:string, :desc=>"key reference"
|
16
|
+
#method_option :value, :alias=>"-v", :type=>:string, :desc=>"values"
|
17
|
+
def set(key,value)
|
18
|
+
puts @client.set_credential(key,value)
|
14
19
|
end
|
15
20
|
|
16
21
|
desc "remove KEY", "remove a value from the credentials bag"
|
17
|
-
def remove
|
22
|
+
def remove(key)
|
23
|
+
puts @client.remove_credential(key)
|
18
24
|
end
|
19
25
|
|
20
26
|
end
|
data/lib/cli/factor_task.rb
CHANGED
@@ -8,34 +8,13 @@ module Factor
|
|
8
8
|
module CLI
|
9
9
|
class FactorTask < Command
|
10
10
|
|
11
|
-
desc "
|
12
|
-
method_option :email, :alias=>"-e", :type=>:string, :desc=>"Email address
|
13
|
-
method_option :
|
14
|
-
def
|
15
|
-
email = options[:email]==nil ? ask("Email address:") : options[:email]
|
16
|
-
password = options[:password]==nil ? ask("Password:") : options[:password]
|
17
|
-
|
18
|
-
# register with email/password
|
19
|
-
|
20
|
-
end
|
21
|
-
|
22
|
-
desc "login", "login to the account"
|
23
|
-
method_option :email, :alias=>"-u", :type=>:string, :desc=>"Account email address"
|
24
|
-
method_option :password, :alias=>"-p", :type=>:string, :desc=>"Account password"
|
25
|
-
def login
|
26
|
-
email = options[:email]==nil ? ask("Email address:") : options[:email]
|
27
|
-
password = options[:password]==nil ? ask("Password:") : options[:password]
|
28
|
-
|
29
|
-
#login with email/password
|
30
|
-
end
|
31
|
-
|
32
|
-
desc "token", "login with token"
|
33
|
-
method_option :email, :alias=>"-e", :type=>:string, :desc=>"Email address for Factor account", :required=>true
|
34
|
-
method_option :token, :alias=>"-t", :type=>:string, :desc=>"Token value to set", :required=>true
|
35
|
-
def token
|
11
|
+
desc "login EMAIL TOKEN", "login with token"
|
12
|
+
# method_option :email, :alias=>"-e", :type=>:string, :desc=>"Email address for Factor account", :required=>true
|
13
|
+
# method_option :token, :alias=>"-t", :type=>:string, :desc=>"Token value to set", :required=>true
|
14
|
+
def login(email,token)
|
36
15
|
config = get_config
|
37
|
-
config[:
|
38
|
-
config[:
|
16
|
+
config[:email]=email
|
17
|
+
config[:token]=token
|
39
18
|
save_config(config)
|
40
19
|
end
|
41
20
|
end
|
@@ -45,3 +24,4 @@ end
|
|
45
24
|
Factor::CLI::FactorTask.register(Factor::CLI::ServerTask, "server","server","start and list servers")
|
46
25
|
Factor::CLI::FactorTask.register(Factor::CLI::WorkflowTask,"workflow","workflow","start and list workflows")
|
47
26
|
Factor::CLI::FactorTask.register(Factor::CLI::ChannelTask,"channel","channel","install,uninstall, list channels")
|
27
|
+
Factor::CLI::FactorTask.register(Factor::CLI::CredentialTask,"credential","credential","manage remote credential store")
|
data/lib/cli/server_task.rb
CHANGED
@@ -7,20 +7,17 @@ module Factor
|
|
7
7
|
desc "start", "start the server"
|
8
8
|
def start
|
9
9
|
engine = Factor::Runtime::Engine.new(get_config[:email],get_config[:token])
|
10
|
-
|
11
|
-
client = Factor::Client::Client.new
|
12
|
-
client.login_token(get_config[:token])
|
13
|
-
|
10
|
+
|
14
11
|
puts "loading channels"
|
15
|
-
engine = client.load_channels(engine)
|
12
|
+
engine = @client.load_channels(engine)
|
16
13
|
puts "loading channels complete"
|
17
14
|
|
18
15
|
puts "loading workflows"
|
19
|
-
engine = client.load_workflows(engine)
|
16
|
+
engine = @client.load_workflows(engine)
|
20
17
|
puts "loading workflows complete"
|
21
18
|
|
22
19
|
puts "loading credentials"
|
23
|
-
engine = client.load_credentials(engine)
|
20
|
+
engine = @client.load_credentials(engine)
|
24
21
|
puts "loading credentials complete"
|
25
22
|
|
26
23
|
puts "starting the server..."
|
data/lib/cli/workflow_task.rb
CHANGED
@@ -16,11 +16,29 @@ module Factor
|
|
16
16
|
puts "workflow executed with id #{id}"
|
17
17
|
end
|
18
18
|
|
19
|
-
desc "list", "list all available workflows"
|
20
|
-
def list
|
21
19
|
|
22
20
|
|
21
|
+
desc "list", "list all the workflows"
|
22
|
+
#method_option :key, :alias=>"-k", :type=>:string, :desc=>"key reference"
|
23
|
+
def list
|
24
|
+
puts @client.get_workflow
|
23
25
|
end
|
26
|
+
|
27
|
+
desc "add NAME FILENAME", "add a key and value for the credential"
|
28
|
+
#method_option :key, :alias=>"-k", :type=>:string, :desc=>"key reference"
|
29
|
+
#method_option :value, :alias=>"-v", :type=>:string, :desc=>"values"
|
30
|
+
def add(name,filename)
|
31
|
+
contents=File.open(File.expand_path(filename), "rb") {|f| f.read}
|
32
|
+
puts @client.add_workflow(name,contents)
|
33
|
+
end
|
34
|
+
|
35
|
+
desc "remove NAME", "remove a workflow"
|
36
|
+
def remove(name)
|
37
|
+
puts @client.remove_workflow(name)
|
38
|
+
end
|
39
|
+
|
40
|
+
|
41
|
+
|
24
42
|
end
|
25
43
|
end
|
26
44
|
end
|
data/lib/client/client.rb
CHANGED
@@ -2,12 +2,13 @@ require 'rubygems'
|
|
2
2
|
require 'rest_client'
|
3
3
|
require 'zip'
|
4
4
|
require 'zip/zipfilesystem'
|
5
|
+
require 'zip/zip'
|
5
6
|
require 'open-uri'
|
6
7
|
|
7
8
|
module Factor
|
8
9
|
module Client
|
9
10
|
class Client
|
10
|
-
HOST = "http://
|
11
|
+
HOST = "http://factor.io"
|
11
12
|
|
12
13
|
def register(email,password)
|
13
14
|
end
|
@@ -30,15 +31,59 @@ module Factor
|
|
30
31
|
end
|
31
32
|
|
32
33
|
def load_credentials(engine)
|
33
|
-
|
34
|
-
credentials_definition = rest_get("credentials")
|
35
|
-
credentials = JSON.parse(credentials_definition["bag"])
|
34
|
+
credentials = rest_get("credentials")
|
36
35
|
engine.load_credentials(credentials)
|
37
36
|
|
38
37
|
engine
|
39
38
|
end
|
40
39
|
|
41
40
|
|
41
|
+
def set_credential(key,value)
|
42
|
+
# this is a PUT not POST because it is technically editing, not creating a new one
|
43
|
+
rest_put("credentials",{:key=>key,:value=>value})
|
44
|
+
end
|
45
|
+
|
46
|
+
def get_credential(key="")
|
47
|
+
rest_get("credentials",{:key=>key})
|
48
|
+
end
|
49
|
+
|
50
|
+
def remove_credential(key="")
|
51
|
+
rest_delete("credentials",{:key=>key})
|
52
|
+
end
|
53
|
+
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
# workflows
|
58
|
+
def add_workflow(key,definition)
|
59
|
+
rest_post("workflows",{:name=>key,:definition=>definition})
|
60
|
+
end
|
61
|
+
|
62
|
+
# def get_workflow()
|
63
|
+
# rest_get("workflows")
|
64
|
+
# end
|
65
|
+
|
66
|
+
def remove_workflow(name="")
|
67
|
+
rest_delete("workflows",{:name=>name})
|
68
|
+
end
|
69
|
+
|
70
|
+
|
71
|
+
# channels
|
72
|
+
def add_channel(key,path)
|
73
|
+
file=zip(path)
|
74
|
+
rest_post("channels",{:name=>key,:zip=>file})
|
75
|
+
end
|
76
|
+
|
77
|
+
# def get_channel()
|
78
|
+
# rest_get("channels")
|
79
|
+
# end
|
80
|
+
|
81
|
+
def remove_channel(name="")
|
82
|
+
rest_delete("channels",{:name=>name})
|
83
|
+
end
|
84
|
+
|
85
|
+
|
86
|
+
|
42
87
|
def load_channels(engine)
|
43
88
|
|
44
89
|
# get list of channels
|
@@ -108,9 +153,46 @@ module Factor
|
|
108
153
|
end
|
109
154
|
FileUtils.rm(zip) if remove_after
|
110
155
|
end
|
156
|
+
|
157
|
+
def zip(directory)
|
158
|
+
path=directory.dup
|
159
|
+
path.sub!(%r[/$],'')
|
160
|
+
zip_path = File.join(path,File.basename(path))+'.zip'
|
161
|
+
FileUtils.rm zip_path, :force=>true
|
162
|
+
Zip::ZipFile.open(zip_path, 'w') do |zip|
|
163
|
+
Dir["#{path}/**/**"].reject{|f|f==zip_path}.each do |file|
|
164
|
+
zip.add(file.sub(path+'/',''),file)
|
165
|
+
end
|
166
|
+
end
|
167
|
+
File.open(zip_path,'r')
|
168
|
+
end
|
111
169
|
|
112
|
-
def rest_get(path)
|
113
|
-
|
170
|
+
def rest_get(path,params={})
|
171
|
+
params["auth_token"]=@token
|
172
|
+
|
173
|
+
param_string=params.map{|key,value| "#{key}=#{value}"}.join("&")
|
174
|
+
|
175
|
+
JSON.parse(RestClient.get("#{HOST}/#{path}.json?#{param_string}"))
|
176
|
+
end
|
177
|
+
|
178
|
+
def rest_put(path,params={})
|
179
|
+
params["auth_token"]=@token
|
180
|
+
|
181
|
+
JSON.parse(RestClient.put("#{HOST}/#{path}.json",params))
|
182
|
+
end
|
183
|
+
|
184
|
+
def rest_post(path,params={})
|
185
|
+
#params["auth_token"]=@token
|
186
|
+
|
187
|
+
JSON.parse(RestClient.post("#{HOST}/#{path}.json?auth_token=#{@token}",params))
|
188
|
+
end
|
189
|
+
|
190
|
+
def rest_delete(path,params={})
|
191
|
+
params["auth_token"]=@token
|
192
|
+
|
193
|
+
param_string=params.map{|key,value| "#{key}=#{value}"}.join("&")
|
194
|
+
|
195
|
+
JSON.parse(RestClient.delete("#{HOST}/#{path}.json?#{param_string}"))
|
114
196
|
end
|
115
197
|
|
116
198
|
end
|
data/lib/runtime/engine.rb
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
require 'factor'
|
3
3
|
require 'mustache'
|
4
|
+
require 'facter'
|
5
|
+
require 'siren'
|
4
6
|
|
5
7
|
|
6
8
|
module Factor
|
@@ -73,21 +75,26 @@ module Factor
|
|
73
75
|
action = activity["action"]
|
74
76
|
channel = activity["channel"]
|
75
77
|
method = activity["method"]
|
78
|
+
target = activity["target"]
|
76
79
|
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
80
|
+
|
81
|
+
if match(target)
|
82
|
+
|
83
|
+
values = message.body.merge(@credentials)
|
84
|
+
# puts "[values] #{values}"
|
85
|
+
# this maps the input values passed in with the templated defined in the workflow
|
86
|
+
params = Hash.new
|
87
|
+
activity["params"].each do |key,template|
|
88
|
+
params[key]=Mustache.render(template,values)
|
89
|
+
end
|
90
|
+
# puts "[calling] #{channel}::#{method} (#{params.to_s})"
|
91
|
+
event = call_channel_method(channel,method,params)
|
86
92
|
|
87
|
-
|
93
|
+
response_message = message.respond(event.params,event.class.name.split("::").last)
|
88
94
|
|
89
95
|
|
90
|
-
|
96
|
+
@message_bus.send response_message
|
97
|
+
end
|
91
98
|
|
92
99
|
end
|
93
100
|
else
|
@@ -97,12 +104,21 @@ module Factor
|
|
97
104
|
end
|
98
105
|
end
|
99
106
|
|
100
|
-
|
101
107
|
def call_channel_method(channel_name,class_name,params)
|
102
108
|
channel_module = @channel_modules[channel_name]
|
103
109
|
command = channel_module.const_get(class_name)
|
104
110
|
command.new.do_work(params)
|
105
111
|
end
|
112
|
+
|
113
|
+
private
|
114
|
+
|
115
|
+
def match(target)
|
116
|
+
return true if target==nil || target==""
|
117
|
+
[key,value]=target.split("=")
|
118
|
+
Facter[key]==value
|
119
|
+
end
|
120
|
+
|
121
|
+
|
106
122
|
end
|
107
123
|
end
|
108
124
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: factor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.7
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -123,6 +123,54 @@ dependencies:
|
|
123
123
|
- - ! '>='
|
124
124
|
- !ruby/object:Gem::Version
|
125
125
|
version: 0.9.8
|
126
|
+
- !ruby/object:Gem::Dependency
|
127
|
+
name: rubyzip
|
128
|
+
requirement: !ruby/object:Gem::Requirement
|
129
|
+
none: false
|
130
|
+
requirements:
|
131
|
+
- - ! '>='
|
132
|
+
- !ruby/object:Gem::Version
|
133
|
+
version: '0'
|
134
|
+
type: :runtime
|
135
|
+
prerelease: false
|
136
|
+
version_requirements: !ruby/object:Gem::Requirement
|
137
|
+
none: false
|
138
|
+
requirements:
|
139
|
+
- - ! '>='
|
140
|
+
- !ruby/object:Gem::Version
|
141
|
+
version: '0'
|
142
|
+
- !ruby/object:Gem::Dependency
|
143
|
+
name: siren
|
144
|
+
requirement: !ruby/object:Gem::Requirement
|
145
|
+
none: false
|
146
|
+
requirements:
|
147
|
+
- - ! '>='
|
148
|
+
- !ruby/object:Gem::Version
|
149
|
+
version: '0'
|
150
|
+
type: :runtime
|
151
|
+
prerelease: false
|
152
|
+
version_requirements: !ruby/object:Gem::Requirement
|
153
|
+
none: false
|
154
|
+
requirements:
|
155
|
+
- - ! '>='
|
156
|
+
- !ruby/object:Gem::Version
|
157
|
+
version: '0'
|
158
|
+
- !ruby/object:Gem::Dependency
|
159
|
+
name: facter
|
160
|
+
requirement: !ruby/object:Gem::Requirement
|
161
|
+
none: false
|
162
|
+
requirements:
|
163
|
+
- - ! '>='
|
164
|
+
- !ruby/object:Gem::Version
|
165
|
+
version: '0'
|
166
|
+
type: :runtime
|
167
|
+
prerelease: false
|
168
|
+
version_requirements: !ruby/object:Gem::Requirement
|
169
|
+
none: false
|
170
|
+
requirements:
|
171
|
+
- - ! '>='
|
172
|
+
- !ruby/object:Gem::Version
|
173
|
+
version: '0'
|
126
174
|
description: Friendly command-line interface and library for Factor
|
127
175
|
email: maciej@skierkowski.com
|
128
176
|
executables:
|