forj 1.0.1 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +7 -0
- data/.gitreview +4 -0
- data/Gemfile +21 -19
- data/Gemfile.lock +71 -0
- data/bin/forj +126 -83
- data/forj.gemspec +64 -0
- data/{lib → forj}/defaults.yaml +23 -1
- data/lib/appinit.rb +5 -5
- data/lib/build_tmpl/build-env.py +293 -0
- data/lib/cloud_test.rb +121 -0
- data/lib/forj-settings.rb +52 -39
- data/lib/forj/ForjCli.rb +11 -10
- data/lib/forj/ForjCore.rb +8 -6
- data/lib/forj/process/ForjProcess.rb +345 -82
- data/lib/ssh.rb +81 -20
- metadata +110 -80
- data/lib/compute.rb +0 -36
- data/lib/connection.rb +0 -144
- data/lib/down.rb +0 -60
- data/lib/forj-account.rb +0 -294
- data/lib/forj-config.rb +0 -522
- data/lib/helpers.rb +0 -56
- data/lib/lib-forj/lib/core/core.rb +0 -1740
- data/lib/lib-forj/lib/core/definition.rb +0 -441
- data/lib/lib-forj/lib/core/definition_internal.rb +0 -306
- data/lib/lib-forj/lib/core_process/CloudProcess.rb +0 -334
- data/lib/lib-forj/lib/core_process/global_process.rb +0 -406
- data/lib/lib-forj/lib/core_process/network_process.rb +0 -603
- data/lib/lib-forj/lib/lib-forj.rb +0 -37
- data/lib/lib-forj/lib/providers/hpcloud/Hpcloud.rb +0 -419
- data/lib/lib-forj/lib/providers/hpcloud/compute.rb +0 -108
- data/lib/lib-forj/lib/providers/hpcloud/network.rb +0 -117
- data/lib/lib-forj/lib/providers/hpcloud/security_groups.rb +0 -67
- data/lib/lib-forj/lib/providers/templates/compute.rb +0 -42
- data/lib/lib-forj/lib/providers/templates/core.rb +0 -61
- data/lib/lib-forj/lib/providers/templates/network.rb +0 -33
- data/lib/log.rb +0 -162
- data/lib/network.rb +0 -365
- data/lib/repositories.rb +0 -222
- data/lib/security.rb +0 -207
- data/lib/ssh.sh +0 -185
- data/spec/connection_spec.rb +0 -52
- data/spec/forj-config_spec.rb +0 -237
- data/spec/repositories_spec.rb +0 -50
@@ -1,108 +0,0 @@
|
|
1
|
-
# encoding: UTF-8
|
2
|
-
|
3
|
-
# (c) Copyright 2014 Hewlett-Packard Development Company, L.P.
|
4
|
-
#
|
5
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
-
# you may not use this file except in compliance with the License.
|
7
|
-
# You may obtain a copy of the License at
|
8
|
-
#
|
9
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
-
#
|
11
|
-
# Unless required by applicable law or agreed to in writing, software
|
12
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
-
# See the License for the specific language governing permissions and
|
15
|
-
# limitations under the License.
|
16
|
-
|
17
|
-
module HPCompute
|
18
|
-
def HPCompute.get_server(oComputeConnect, sId)
|
19
|
-
oComputeConnect.servers.get(sId)
|
20
|
-
end
|
21
|
-
|
22
|
-
def HPCompute.query_addresses(oComputeConnect, sQuery)
|
23
|
-
oComputeConnect.addresses.all(sQuery)
|
24
|
-
end
|
25
|
-
|
26
|
-
def HPCompute.query_server(oComputeConnect, sQuery)
|
27
|
-
oComputeConnect.servers.all(sQuery)
|
28
|
-
end
|
29
|
-
|
30
|
-
def HPCompute.query_image(oComputeConnect, sQuery)
|
31
|
-
# HP Fog query is exact matching. No way to filter with a Regexp
|
32
|
-
# Testing it and filtering it.
|
33
|
-
# TODO: Be able to support Regexp in queries then extract all and filter.
|
34
|
-
oComputeConnect.images.all(sQuery)
|
35
|
-
end
|
36
|
-
|
37
|
-
def HPCompute.query_flavor(oComputeConnect, sQuery)
|
38
|
-
oComputeConnect.flavors.all(sQuery)
|
39
|
-
end
|
40
|
-
|
41
|
-
def HPCompute.create_server(oComputeConnect,
|
42
|
-
sServerName, oSecurity_groups,
|
43
|
-
oImage, oNetwork,
|
44
|
-
oFlavor, oKeypairs,
|
45
|
-
oUser_data, oMeta_data)
|
46
|
-
|
47
|
-
options = {
|
48
|
-
:name => sServerName,
|
49
|
-
:flavor_id => oFlavor.id,
|
50
|
-
:image_id => oImage.id,
|
51
|
-
:key_name => oKeypairs.name,
|
52
|
-
:security_groups => [oSecurity_groups.name],
|
53
|
-
:networks => [oNetwork.id]
|
54
|
-
}
|
55
|
-
options[:user_data_encoded] = Base64.strict_encode64(oUser_data) if oUser_data
|
56
|
-
options[:metadata] = oMeta_data if oMeta_data
|
57
|
-
server = oComputeConnect.servers.create(options)
|
58
|
-
HPCompute.get_server(oComputeConnect, server.id ) if server
|
59
|
-
end
|
60
|
-
|
61
|
-
def HPCompute.query_server_assigned_addresses(oComputeConnect, oServer, sQuery)
|
62
|
-
# CloudProcess used a simplified way to manage IPs.
|
63
|
-
# Following is the translation to get the public IPs for the server
|
64
|
-
|
65
|
-
result = []
|
66
|
-
oAddresses = oComputeConnect.addresses.all()
|
67
|
-
oAddresses.each { | oElem |
|
68
|
-
bFound = true
|
69
|
-
sQuery.each { | key, value |
|
70
|
-
if not oElem.attributes.key?(key) or oElem.attributes[key] != value
|
71
|
-
bFound = false
|
72
|
-
break
|
73
|
-
end
|
74
|
-
}
|
75
|
-
result << oElem if bFound
|
76
|
-
}
|
77
|
-
result
|
78
|
-
end
|
79
|
-
|
80
|
-
def HPCompute.server_assign_address(oComputeConnect, oServer)
|
81
|
-
|
82
|
-
while oServer.state != 'ACTIVE'
|
83
|
-
sleep(5)
|
84
|
-
oServer = oComputeConnect.servers.get(oServer.id)
|
85
|
-
end
|
86
|
-
|
87
|
-
oAddresses = oComputeConnect.addresses.all()
|
88
|
-
oAddress = nil
|
89
|
-
# Search for an available IP
|
90
|
-
oAddresses.each { | oElem |
|
91
|
-
if oElem.fixed_ip.nil?
|
92
|
-
oAddress = oElem
|
93
|
-
break
|
94
|
-
end
|
95
|
-
}
|
96
|
-
|
97
|
-
if oAddress.nil?
|
98
|
-
# Create a new public IP to add in the pool.
|
99
|
-
oAddress = oComputeConnect.addresses.create
|
100
|
-
end
|
101
|
-
raise "No Public IP to assign to server '%s'" % oServer.name if oAddress.nil?
|
102
|
-
oAddress.server = oServer # associate the server
|
103
|
-
oAddress.reload
|
104
|
-
# This function needs to returns a list of object.
|
105
|
-
# This list must support the each function.
|
106
|
-
oAddress
|
107
|
-
end
|
108
|
-
end
|
@@ -1,117 +0,0 @@
|
|
1
|
-
# encoding: UTF-8
|
2
|
-
|
3
|
-
# (c) Copyright 2014 Hewlett-Packard Development Company, L.P.
|
4
|
-
#
|
5
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
-
# you may not use this file except in compliance with the License.
|
7
|
-
# You may obtain a copy of the License at
|
8
|
-
#
|
9
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
-
#
|
11
|
-
# Unless required by applicable law or agreed to in writing, software
|
12
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
-
# See the License for the specific language governing permissions and
|
15
|
-
# limitations under the License.
|
16
|
-
|
17
|
-
|
18
|
-
module HPNetwork
|
19
|
-
# Network driver
|
20
|
-
def HPNetwork.query_network(oNetworkConnect, sQuery)
|
21
|
-
oNetworkConnect.networks.all(sQuery)
|
22
|
-
end
|
23
|
-
|
24
|
-
def HPNetwork.create_network(oNetworkConnect, name)
|
25
|
-
oNetworkConnect.networks.create(:name => name)
|
26
|
-
end
|
27
|
-
|
28
|
-
def HPNetwork.delete_network(oNetworkConnect, oNetwork)
|
29
|
-
oNetworkConnect.networks.get(oNetwork.id).destroy
|
30
|
-
end
|
31
|
-
|
32
|
-
# SubNetwork driver
|
33
|
-
def HPNetwork.query_subnetwork(oNetworkConnect, sQuery)
|
34
|
-
oNetworkConnect.subnets.all(sQuery)
|
35
|
-
end
|
36
|
-
|
37
|
-
def HPNetwork.create_subnetwork(oNetworkConnect, oNetwork, name)
|
38
|
-
oNetworkConnect.subnets.create(
|
39
|
-
:network_id => oNetwork.id,
|
40
|
-
:name => name,
|
41
|
-
:cidr => get_next_subnet(oNetworkConnect),
|
42
|
-
:ip_version => '4'
|
43
|
-
)
|
44
|
-
end
|
45
|
-
|
46
|
-
def HPNetwork.delete_subnetwork(oNetworkConnect, oSubNetwork)
|
47
|
-
oNetworkConnect.subnets.get(oSubNetwork.id).destroy
|
48
|
-
end
|
49
|
-
|
50
|
-
def HPNetwork.get_next_subnet(oNetworkConnect)
|
51
|
-
begin
|
52
|
-
subnet_values = Array.new
|
53
|
-
subnets = oNetworkConnect.subnets.all
|
54
|
-
|
55
|
-
subnets.each do|s|
|
56
|
-
subnet_values.push(s.cidr)
|
57
|
-
end
|
58
|
-
|
59
|
-
gap = false
|
60
|
-
count = 0
|
61
|
-
range_used = Array.new
|
62
|
-
new_subnet = 0
|
63
|
-
new_cidr = ''
|
64
|
-
|
65
|
-
subnet_values = subnet_values.sort!
|
66
|
-
|
67
|
-
subnet_values.each do|value|
|
68
|
-
range_used.push(value[5])
|
69
|
-
end
|
70
|
-
|
71
|
-
range_used.each do |n|
|
72
|
-
if count.to_i == n.to_i
|
73
|
-
else
|
74
|
-
new_subnet = count
|
75
|
-
gap = true
|
76
|
-
break
|
77
|
-
end
|
78
|
-
count += 1
|
79
|
-
end
|
80
|
-
|
81
|
-
if gap
|
82
|
-
new_cidr = '10.0.%s.0/24' % [count]
|
83
|
-
else
|
84
|
-
max_value = range_used.max
|
85
|
-
new_subnet = max_value.to_i + 1
|
86
|
-
new_cidr = '10.0.%s.0/24' % [new_subnet]
|
87
|
-
end
|
88
|
-
new_cidr
|
89
|
-
rescue => e
|
90
|
-
Logging.error("%s\n%s" % [e.message, e.backtrace.join("\n")])
|
91
|
-
end
|
92
|
-
end
|
93
|
-
|
94
|
-
# router driver
|
95
|
-
def HPNetwork.query_router(oNetworkConnect, sQuery)
|
96
|
-
oNetworkConnect.routers.all(sQuery)
|
97
|
-
end
|
98
|
-
|
99
|
-
def HPNetwork.update_router(oRouters)
|
100
|
-
oRouters.save
|
101
|
-
end
|
102
|
-
|
103
|
-
def HPNetwork.create_router(oNetwork, hOptions)
|
104
|
-
oNetwork.routers.create(hOptions)
|
105
|
-
end
|
106
|
-
|
107
|
-
# router interface
|
108
|
-
|
109
|
-
def HPNetwork.add_interface(oRouter, oSubNetwork)
|
110
|
-
oRouter.add_interface(oSubNetwork.id, nil)
|
111
|
-
end
|
112
|
-
|
113
|
-
# Port driver
|
114
|
-
def HPNetwork.query_port(oNetworkConnect, sQuery)
|
115
|
-
oNetworkConnect.ports.all(sQuery)
|
116
|
-
end
|
117
|
-
end
|
@@ -1,67 +0,0 @@
|
|
1
|
-
# encoding: UTF-8
|
2
|
-
|
3
|
-
# (c) Copyright 2014 Hewlett-Packard Development Company, L.P.
|
4
|
-
#
|
5
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
-
# you may not use this file except in compliance with the License.
|
7
|
-
# You may obtain a copy of the License at
|
8
|
-
#
|
9
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
-
#
|
11
|
-
# Unless required by applicable law or agreed to in writing, software
|
12
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
-
# See the License for the specific language governing permissions and
|
15
|
-
# limitations under the License.
|
16
|
-
|
17
|
-
module HPSecurityGroups
|
18
|
-
def HPSecurityGroups.query_sg(oNetworkConnect, sQuery)
|
19
|
-
oNetworkConnect.security_groups.all(sQuery)
|
20
|
-
end
|
21
|
-
|
22
|
-
def HPSecurityGroups.create_sg(oNetwork, name, description)
|
23
|
-
params = {:name => name}
|
24
|
-
params[:description] = description if description
|
25
|
-
oNetwork.security_groups.create( params )
|
26
|
-
end
|
27
|
-
|
28
|
-
def HPSecurityGroups.create_rule(oNetwork, hData)
|
29
|
-
oNetwork.security_group_rules.create( hData )
|
30
|
-
end
|
31
|
-
|
32
|
-
def HPSecurityGroups.query_rule(oNetwork, sQuery)
|
33
|
-
oNetwork.security_group_rules.all(sQuery)
|
34
|
-
end
|
35
|
-
|
36
|
-
def HPSecurityGroups.delete_rule(oNetwork, rule_id)
|
37
|
-
oNetwork.security_group_rules.get(rule_id).destroy
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
module HPKeyPairs
|
42
|
-
def HPKeyPairs.query_keypair(oComputeConnect, sQuery)
|
43
|
-
cKeyPairs = oComputeConnect.key_pairs.all()
|
44
|
-
aResults = []
|
45
|
-
cKeyPairs.each { |sElem|
|
46
|
-
bSelect = true
|
47
|
-
attributes = sElem.instance_variable_get(:@attributes)
|
48
|
-
sQuery.each { | key, value |
|
49
|
-
if attributes[key] != value
|
50
|
-
bSelect = false
|
51
|
-
break
|
52
|
-
end
|
53
|
-
}
|
54
|
-
aResults.push sElem if bSelect
|
55
|
-
}
|
56
|
-
aResults
|
57
|
-
end
|
58
|
-
|
59
|
-
def HPKeyPairs.get_keypair(oComputeConnect, sId)
|
60
|
-
#byebug
|
61
|
-
oComputeConnect.key_pairs.get(sId)
|
62
|
-
end
|
63
|
-
|
64
|
-
def HPKeyPairs.create_keypair(oComputeConnect, name, pubkey)
|
65
|
-
oComputeConnect.key_pairs.create( :name => name, :public_key => pubkey)
|
66
|
-
end
|
67
|
-
end
|
@@ -1,42 +0,0 @@
|
|
1
|
-
# encoding: UTF-8
|
2
|
-
|
3
|
-
# (c) Copyright 2014 Hewlett-Packard Development Company, L.P.
|
4
|
-
#
|
5
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
-
# you may not use this file except in compliance with the License.
|
7
|
-
# You may obtain a copy of the License at
|
8
|
-
#
|
9
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
-
#
|
11
|
-
# Unless required by applicable law or agreed to in writing, software
|
12
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
-
# See the License for the specific language governing permissions and
|
15
|
-
# limitations under the License.
|
16
|
-
|
17
|
-
# This file is given as an example.
|
18
|
-
|
19
|
-
class Mycloud # This class is automatically derived from ForjCloudBase and ForjProcess
|
20
|
-
|
21
|
-
def provider_compute_new
|
22
|
-
# My fog connection
|
23
|
-
# hget_cloudObjMapping() is a ForjCloudBase function which will build a
|
24
|
-
# hash from data required with needed mapped keys(see core.rb)
|
25
|
-
Fog::Compute.new({:provider => :mycloud}.merge(hget_cloudObjMapping()))
|
26
|
-
|
27
|
-
# If you do not want to get data mapped automatically, you can use
|
28
|
-
# @oForjAccount.get()
|
29
|
-
# This means in following declaration in your core.rb:
|
30
|
-
# obj_needs(:data, :<CloudDataKey},{:mapping => :<MyCloudKeyMapped>})
|
31
|
-
# can be updated by removing the mapping => <Value>
|
32
|
-
Fog::Compute.new({
|
33
|
-
:provider => :mycloud,
|
34
|
-
:user => @oForjAccount.get(:account_id),
|
35
|
-
:pwd => @oForjAccount.get(:account_key),
|
36
|
-
:auth_uri => @oForjAccount.get(:auth_uri),
|
37
|
-
:project => @oForjAccount.get(:tenant),
|
38
|
-
:compute_service => @oForjAccount.get(:compute),
|
39
|
-
})
|
40
|
-
|
41
|
-
end
|
42
|
-
end
|
@@ -1,61 +0,0 @@
|
|
1
|
-
# encoding: UTF-8
|
2
|
-
|
3
|
-
# (c) Copyright 2014 Hewlett-Packard Development Company, L.P.
|
4
|
-
#
|
5
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
-
# you may not use this file except in compliance with the License.
|
7
|
-
# You may obtain a copy of the License at
|
8
|
-
#
|
9
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
-
#
|
11
|
-
# Unless required by applicable law or agreed to in writing, software
|
12
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
-
# See the License for the specific language governing permissions and
|
15
|
-
# limitations under the License.
|
16
|
-
|
17
|
-
|
18
|
-
# This file is given as an example.
|
19
|
-
|
20
|
-
require File.join($PROVIDER_PATH, "compute.rb")
|
21
|
-
require File.join($PROVIDER_PATH, "network.rb")
|
22
|
-
|
23
|
-
# Defines Meta MyCloud object
|
24
|
-
class Mycloud
|
25
|
-
|
26
|
-
# Defines Object structure and function stored on the Hpcloud class object.
|
27
|
-
|
28
|
-
# ForjCloud has a list of predefined object, like compute_connection, network, ...
|
29
|
-
# See lib/providers/core/cloud_data_pref.rb
|
30
|
-
|
31
|
-
# Compute Object
|
32
|
-
define_obj :compute_connection
|
33
|
-
# Defines Data used by compute.
|
34
|
-
obj_needs(:data, :account_id, { :mapping => :user})
|
35
|
-
obj_needs(:data, :account_key, { :mapping => :pwd})
|
36
|
-
obj_needs(:data, :auth_uri, { :mapping => :auth_uri})
|
37
|
-
obj_needs(:data, :tenant, { :mapping => :project})
|
38
|
-
obj_needs(:data, :compute, { :mapping => :compute_service})
|
39
|
-
|
40
|
-
define_obj :network_connection
|
41
|
-
obj_needs(:data, :account_id, { :mapping => :user})
|
42
|
-
obj_needs(:data, :account_key, { :mapping => :pwd})
|
43
|
-
obj_needs(:data, :auth_uri, { :mapping => :auth_uri})
|
44
|
-
obj_needs(:data, :tenant, { :mapping => :project})
|
45
|
-
obj_needs(:data, :network, { :mapping => :network_service})
|
46
|
-
|
47
|
-
define_obj :network
|
48
|
-
obj_needs(:CloudObject, :network_connection)
|
49
|
-
obj_needs(:data, :network_name)
|
50
|
-
|
51
|
-
# defines setup Cloud data
|
52
|
-
# This definition is required only if you need to change the predefined data.
|
53
|
-
# To get details on what kind of parameters can be applied to a CloudData, see lib/defaults.yaml
|
54
|
-
define_data(:account_id, {:provisioned_by => :setup, :desc => 'MyCloud username'})
|
55
|
-
define_data(:account_key, {:provisioned_by => :setup, :desc => 'HPCloud secret Key'})
|
56
|
-
define_data(:auth_uri, {:provisioned_by => :setup, :desc => 'HPCloud Authentication service URL'})
|
57
|
-
define_data(:tenant, {:provisioned_by => :setup, :desc => 'HPCloud Tenant ID'})
|
58
|
-
define_data(:compute, {:provisioned_by => :setup, :desc => 'HPCloud Compute service zone (Ex: region-a.geo-1)'})
|
59
|
-
define_data(:network, {:provisioned_by => :setup, :desc => 'HPCloud Network service zone (Ex: region-a.geo-1)'})
|
60
|
-
|
61
|
-
end
|
@@ -1,33 +0,0 @@
|
|
1
|
-
# encoding: UTF-8
|
2
|
-
|
3
|
-
# (c) Copyright 2014 Hewlett-Packard Development Company, L.P.
|
4
|
-
#
|
5
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
-
# you may not use this file except in compliance with the License.
|
7
|
-
# You may obtain a copy of the License at
|
8
|
-
#
|
9
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
-
#
|
11
|
-
# Unless required by applicable law or agreed to in writing, software
|
12
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
-
# See the License for the specific language governing permissions and
|
15
|
-
# limitations under the License.
|
16
|
-
|
17
|
-
# This file is given as an example.
|
18
|
-
|
19
|
-
class Mycloud # This class is automatically derived from ForjCloudBase and ForjProcess
|
20
|
-
|
21
|
-
def provider_network_new
|
22
|
-
Fog::Network.new({:provider => :mycloud}.merge(hget_cloudObjMapping()))
|
23
|
-
end
|
24
|
-
|
25
|
-
def provider_query_network(oNetwork, name)
|
26
|
-
oNetwork.networks.all(:name => name)
|
27
|
-
end
|
28
|
-
|
29
|
-
def provider_create_network(oNetwork, name)
|
30
|
-
oNetwork.networks.create(:name => name)
|
31
|
-
end
|
32
|
-
|
33
|
-
end
|
data/lib/log.rb
DELETED
@@ -1,162 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
# encoding: UTF-8
|
3
|
-
|
4
|
-
# (c) Copyright 2014 Hewlett-Packard Development Company, L.P.
|
5
|
-
#
|
6
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
7
|
-
# you may not use this file except in compliance with the License.
|
8
|
-
# You may obtain a copy of the License at
|
9
|
-
#
|
10
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
11
|
-
#
|
12
|
-
# Unless required by applicable law or agreed to in writing, software
|
13
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
14
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15
|
-
# See the License for the specific language governing permissions and
|
16
|
-
# limitations under the License.
|
17
|
-
|
18
|
-
|
19
|
-
# create a forj.log file in ~/.hpcloud/forj.log
|
20
|
-
|
21
|
-
require 'rubygems'
|
22
|
-
require 'logger'
|
23
|
-
require 'ansi'
|
24
|
-
require 'ansi/logger'
|
25
|
-
|
26
|
-
require 'require_relative'
|
27
|
-
|
28
|
-
require_relative 'helpers.rb'
|
29
|
-
include Helpers
|
30
|
-
|
31
|
-
|
32
|
-
#
|
33
|
-
# Logging module
|
34
|
-
#
|
35
|
-
module Logging
|
36
|
-
|
37
|
-
class ForjLog
|
38
|
-
# Class used to create 2 log object, in order to keep track of error in a log file and change log output to OUTPUT on needs (option flags).
|
39
|
-
|
40
|
-
attr_reader :level
|
41
|
-
|
42
|
-
def initialize(sLogFile = 'forj.log', level = Logger::WARN)
|
43
|
-
|
44
|
-
if not $FORJ_DATA_PATH
|
45
|
-
raise "Internal Error: Unable to initialize ForjLog - global FORJ_DATA_PATH not set"
|
46
|
-
end
|
47
|
-
|
48
|
-
if not Helpers.dir_exists?($FORJ_DATA_PATH)
|
49
|
-
raise "Internal Error: Unable to initialize ForjLog - '%s' doesn't exist." % $FORJ_DATA_PATH
|
50
|
-
end
|
51
|
-
|
52
|
-
@oFileLogger = Logger.new(File.join($FORJ_DATA_PATH, sLogFile), 'weekly')
|
53
|
-
@oFileLogger.level = Logger::DEBUG
|
54
|
-
@oFileLogger.formatter = proc do |severity, datetime, progname, msg|
|
55
|
-
"#{progname} : #{datetime}: #{severity}: #{msg} \n"
|
56
|
-
end
|
57
|
-
|
58
|
-
@oOutLogger = Logger.new(STDOUT)
|
59
|
-
@level = level
|
60
|
-
@oOutLogger.level = @level
|
61
|
-
@oOutLogger.formatter = proc do |severity, datetime, progname, msg|
|
62
|
-
case severity
|
63
|
-
when 'ANY'
|
64
|
-
str = "#{msg} \n"
|
65
|
-
when "ERROR", "FATAL"
|
66
|
-
str = ANSI.bold(ANSI.red("#{severity}!!!")) + ": #{msg} \n"
|
67
|
-
when "WARN"
|
68
|
-
str = ANSI.bold(ANSI.yellow("WARNING")) + ": #{msg} \n"
|
69
|
-
else
|
70
|
-
str = "#{severity}: #{msg} \n"
|
71
|
-
end
|
72
|
-
str
|
73
|
-
end
|
74
|
-
end
|
75
|
-
|
76
|
-
def info?
|
77
|
-
return(@oOutLogger.info?)
|
78
|
-
end
|
79
|
-
def debug?
|
80
|
-
return(@oOutLogger.debug?)
|
81
|
-
end
|
82
|
-
def error?
|
83
|
-
return(@oOutLogger.error?)
|
84
|
-
end
|
85
|
-
def fatal?
|
86
|
-
return(@oOutLogger.fatal?)
|
87
|
-
end
|
88
|
-
|
89
|
-
def info(message)
|
90
|
-
@oOutLogger.info(message + ANSI.clear_line)
|
91
|
-
@oFileLogger.info(message)
|
92
|
-
end
|
93
|
-
def debug(message)
|
94
|
-
@oOutLogger.debug(message + ANSI.clear_line)
|
95
|
-
@oFileLogger.debug(message)
|
96
|
-
end
|
97
|
-
def error(message)
|
98
|
-
@oOutLogger.error(message + ANSI.clear_line)
|
99
|
-
@oFileLogger.error(message)
|
100
|
-
end
|
101
|
-
def fatal(message, e)
|
102
|
-
@oOutLogger.fatal(message + ANSI.clear_line)
|
103
|
-
@oFileLogger.fatal("%s\n%s\n%s" % [message, e.message, e.backtrace.join("\n")]) if e
|
104
|
-
@oFileLogger.fatal(message)
|
105
|
-
end
|
106
|
-
|
107
|
-
def warn(message)
|
108
|
-
@oOutLogger.warn(message + ANSI.clear_line)
|
109
|
-
@oFileLogger.warn(message)
|
110
|
-
end
|
111
|
-
|
112
|
-
def set_level(level)
|
113
|
-
@level = level
|
114
|
-
@oOutLogger.level = level
|
115
|
-
end
|
116
|
-
|
117
|
-
def unknown(message)
|
118
|
-
@oOutLogger.unknown(message + ANSI.clear_line)
|
119
|
-
end
|
120
|
-
|
121
|
-
end
|
122
|
-
|
123
|
-
def message(message)
|
124
|
-
$FORJ_LOGGER.unknown(message)
|
125
|
-
end
|
126
|
-
|
127
|
-
def info(message)
|
128
|
-
$FORJ_LOGGER.info(message)
|
129
|
-
end
|
130
|
-
|
131
|
-
def debug(message)
|
132
|
-
$FORJ_LOGGER.debug(message)
|
133
|
-
end
|
134
|
-
|
135
|
-
def warning(message)
|
136
|
-
$FORJ_LOGGER.warn(message)
|
137
|
-
end
|
138
|
-
|
139
|
-
def error(message)
|
140
|
-
$FORJ_LOGGER.error(message)
|
141
|
-
end
|
142
|
-
|
143
|
-
def fatal(rc, message, e = nil)
|
144
|
-
$FORJ_LOGGER.fatal(message, e)
|
145
|
-
puts 'Issues found. Please fix it and retry. Process aborted.'
|
146
|
-
exit rc
|
147
|
-
end
|
148
|
-
|
149
|
-
def set_level(level)
|
150
|
-
$FORJ_LOGGER.set_level(level)
|
151
|
-
end
|
152
|
-
|
153
|
-
def state(message)
|
154
|
-
print("%s ...%s\r" % [message, ANSI.clear_line]) if $FORJ_LOGGER.level <= Logger::INFO
|
155
|
-
end
|
156
|
-
|
157
|
-
def high_level_msg(message)
|
158
|
-
# Not DEBUG and not INFO. Just printed to the output.
|
159
|
-
print ("%s" % [message]) if $FORJ_LOGGER.level > 1
|
160
|
-
end
|
161
|
-
|
162
|
-
end
|