Class: Phper::Agent
- Inherits:
-
Object
show all
- Includes:
- Phper
- Defined in:
- lib/phper/agent.rb
Instance Method Summary
collapse
-
#base_url ⇒ Object
-
#delete(url, data = {}, format = :json) ⇒ Object
-
#exec(project, command) ⇒ Object
-
#files(project, host, name = nil) ⇒ Object
-
#get(url, data = {}, format = :json) ⇒ Object
-
#hosts(project) ⇒ Object
-
#initialize ⇒ Agent
constructor
-
#keys ⇒ Object
-
#keys_create(key) ⇒ Object
-
#keys_delete(name) ⇒ Object
-
#keys_delete_all ⇒ Object
-
#log(dest) ⇒ Object
-
#login(u, p) ⇒ Object
-
#login? ⇒ Boolean
-
#logs(project, server) ⇒ Object
-
#logs_tail(project, server, name) ⇒ Object
-
#post(url, data = {}, format = :json) ⇒ Object
-
#project_from_git(git) ⇒ Object
-
#projects(id = nil) ⇒ Object
-
#projects_create(name) ⇒ Object
-
#projects_delete(name) ⇒ Object
-
#projects_deploy(project) ⇒ Object
-
#projects_init_db(project) ⇒ Object
-
#put(url, data = {}, format = :json) ⇒ Object
-
#servers(project) ⇒ Object
-
#servers_create(project, param) ⇒ Object
-
#servers_delete(project, name) ⇒ Object
-
#version_set(project, ver) ⇒ Object
-
#versions(project) ⇒ Object
Methods included from Phper
#decode, #encode, #git_remote, #git_remotes, #git_root, #in_git?, #name_of_key
Constructor Details
#initialize ⇒ Agent
Returns a new instance of Agent.
7
8
9
|
# File 'lib/phper/agent.rb', line 7
def initialize
@login = false
end
|
Instance Method Details
#base_url ⇒ Object
167
168
169
|
# File 'lib/phper/agent.rb', line 167
def base_url
ENV["PHPERJP_URL"] || "https://phper.jp"
end
|
#delete(url, data = {}, format = :json) ⇒ Object
163
164
165
|
# File 'lib/phper/agent.rb', line 163
def delete(url,data = {},format=:json)
call(:delete,url,data,format)
end
|
#exec(project, command) ⇒ Object
69
70
71
|
# File 'lib/phper/agent.rb', line 69
def exec project, command
post("/projects/#{project}/exec", {:command => command})
end
|
#files(project, host, name = nil) ⇒ Object
139
140
141
142
143
144
145
|
# File 'lib/phper/agent.rb', line 139
def files project,host,name = nil
if name
get("/projects/%s/hosts/%s/files/%s" % [project,host,encode(name)])
else
get("/projects/%s/hosts/%s/files" % [project,host])
end
end
|
#get(url, data = {}, format = :json) ⇒ Object
155
156
157
|
# File 'lib/phper/agent.rb', line 155
def get(url,data = {},format=:json)
call(:get,url,data,format)
end
|
#hosts(project) ⇒ Object
135
136
137
|
# File 'lib/phper/agent.rb', line 135
def hosts project
get("/projects/%s/hosts" % project)
end
|
#keys ⇒ Object
53
54
55
|
# File 'lib/phper/agent.rb', line 53
def keys
get("/keys")
end
|
#keys_create(key) ⇒ Object
86
87
88
89
90
|
# File 'lib/phper/agent.rb', line 86
def keys_create key
param = {}
param[:public_key] = key if key
post("/keys",{:key => param})
end
|
#keys_delete(name) ⇒ Object
92
93
94
95
96
97
98
99
100
101
|
# File 'lib/phper/agent.rb', line 92
def keys_delete name
count = 0
keys.each { |key|
if name == name_of_key(key["key"]["public_key"])
delete("/keys/%i" % key["key"]["id"])
count += 1
end
}
count
end
|
#keys_delete_all ⇒ Object
103
104
105
106
107
108
109
110
|
# File 'lib/phper/agent.rb', line 103
def keys_delete_all
count = 0
keys.each { |key|
delete("/keys/%i" % key["key"]["id"])
count += 1
}
count
end
|
#log(dest) ⇒ Object
11
12
13
|
# File 'lib/phper/agent.rb', line 11
def log dest
RestClient.log = dest
end
|
#login(u, p) ⇒ Object
15
16
17
18
19
20
21
22
23
24
|
# File 'lib/phper/agent.rb', line 15
def login(u,p)
@auth = {:user => u, :password => p}
begin
projects
@login = true
rescue => e
puts e
@login = false
end
end
|
#login? ⇒ Boolean
26
27
28
|
# File 'lib/phper/agent.rb', line 26
def login?
@login
end
|
#logs(project, server) ⇒ Object
57
58
59
|
# File 'lib/phper/agent.rb', line 57
def logs project, server
get("/projects/%s/servers/%s/logs" % [project,server["server"]["id"]])
end
|
#logs_tail(project, server, name) ⇒ Object
73
74
75
76
|
# File 'lib/phper/agent.rb', line 73
def logs_tail project,server,name
get("/projects/%s/servers/%s/logs/%s" %
[project,server["server"]["id"],name])
end
|
#post(url, data = {}, format = :json) ⇒ Object
151
152
153
|
# File 'lib/phper/agent.rb', line 151
def post(url,data = {},format=:json)
call(:post,url,data,format)
end
|
#project_from_git(git) ⇒ Object
31
32
33
|
# File 'lib/phper/agent.rb', line 31
def project_from_git git
get("/gits/#{git}")
end
|
#projects(id = nil) ⇒ Object
35
36
37
38
39
40
41
|
# File 'lib/phper/agent.rb', line 35
def projects id = nil
if id
get("/projects/%s" % id)
else
get("/projects")
end
end
|
#projects_create(name) ⇒ Object
43
44
45
46
47
|
# File 'lib/phper/agent.rb', line 43
def projects_create name
param = {}
param[:name] = name if name
post("/projects",{:project => param})
end
|
#projects_delete(name) ⇒ Object
49
50
51
|
# File 'lib/phper/agent.rb', line 49
def projects_delete name
delete("/projects/#{name}")
end
|
#projects_deploy(project) ⇒ Object
78
79
80
|
# File 'lib/phper/agent.rb', line 78
def projects_deploy project
get("/projects/%s/deploy" % project)
end
|
#projects_init_db(project) ⇒ Object
82
83
84
|
# File 'lib/phper/agent.rb', line 82
def projects_init_db project
get("/projects/%s/db/initialize" % project)
end
|
#put(url, data = {}, format = :json) ⇒ Object
159
160
161
|
# File 'lib/phper/agent.rb', line 159
def put(url,data = {},format=:json)
call(:put,url,data,format)
end
|
#servers(project) ⇒ Object
112
113
114
|
# File 'lib/phper/agent.rb', line 112
def servers project
get("/projects/%s/servers" % project)
end
|
#servers_create(project, param) ⇒ Object
116
117
118
119
120
121
|
# File 'lib/phper/agent.rb', line 116
def servers_create project,param
defaults = get("/projects/%s/servers/new" % project)["server"]
param[:name] = defaults["name"] unless param.has_key?(:name)
param[:fqdn] = defaults["fqdn"] unless param.has_key?(:fqdn)
post("/projects/%s/servers" % project ,{:server => param})
end
|
#servers_delete(project, name) ⇒ Object
124
125
126
127
128
129
130
131
132
133
|
# File 'lib/phper/agent.rb', line 124
def servers_delete project,name
count = 0;
servers(project).each { |s|
if s["server"]["fqdn"] == name or s["server"]["name"] == name
delete("/projects/%s/servers/%s"%[project,s["server"]["id"]])
count += 1
end
}
count
end
|
#version_set(project, ver) ⇒ Object
65
66
67
|
# File 'lib/phper/agent.rb', line 65
def version_set project, ver
put("/projects/#{project}/versions", {:version => ver})
end
|
#versions(project) ⇒ Object
61
62
63
|
# File 'lib/phper/agent.rb', line 61
def versions project
get("/projects/%s/versions" % [project])
end
|