astroboa-cli 0.3.0

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.
@@ -0,0 +1,443 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'astroboa-cli/command/base'
4
+ require 'fileutils'
5
+ require 'nokogiri'
6
+
7
+ # create, delete, backup, restore repositories
8
+ class AstroboaCLI::Command::Repository < AstroboaCLI::Command::Base
9
+
10
+ # repository:create REPOSITORY_NAME
11
+ #
12
+ # Creates a new astroboa repository with the provided 'REPOSITORY_NAME'.
13
+ # The 'REPOSITORY_NAME' is the internal repository name.
14
+ # It should be at least 3 characters long and should not contain spaces and special symbols (!#@?-*).
15
+ # Only english word characters are allowed (a-z or A-Z or 0-9 or _)
16
+ # You may use the -l option to provide friendly localized (for different languages) labels with spaces
17
+ # A directory named after the name of the new repository will be created to store repository configuration, indexes and blobs.
18
+ # This directory will be created under the global repositories directory that has been setup during astroboa server installation.
19
+ # A database will also be created to store repository data (except blobs).
20
+ # The database server, db user and db password that have been configured during astroboa server setup will be used.
21
+ #
22
+ # -l, --localized_labels REPO_LABELS # Provide friendly labels for different languages # By default the 'REPOSITORY_NAME' will be used as the english label # Example: -l en:Dali Paintings,fr:Dali Peintures,es:Dali Pinturas
23
+ # -d, --domain_name REPO_DOMAIN_NAME # Specify the fully qualified domain name and port under which all the repository resources will be exposed by the REST API # The default is 'localhost:8080' # If you specify other than the default (e.g. www.mydomain.com) you need to appropriately setup a reverse proxy in front of astroboa
24
+ # -p, --api_base_path REST_API_BASE_PATH # Specify the base_path of the REST API URLs # The default is '/resource-api' # If you provide other than the default you should appropriately setup a reverse proxy in front of astroboa
25
+ # -n, --db_name DATABASE_NAME # Specify the name of the database to create # Default is REPOSITORY_NAME (i.e. the name of the new repository)
26
+ #
27
+ #Examples:
28
+ #
29
+ # Create the repository 'dali_paintings'.
30
+ # It will be stored in db named 'dali_paintings' and its RESOURCES
31
+ # will be available under http://localhost:8080/resource-api/dali_paintings
32
+ # $ astroboa-cli repository:create dali_paintings
33
+ #
34
+ # Specify i18n labels for the repository name.
35
+ # Expose the repo resources under http://www.art.com/art-api/dali_paintings
36
+ # $ astroboa-cli repository:create dali_paintings -l en:Dali Paintings,fr:Dali Peintures,es:Dali Pinturas -d www.art.com -p art-api
37
+ #
38
+ def create
39
+
40
+ if repository_name = args.shift
41
+ repository_name = repository_name.strip
42
+ else
43
+ error "Please specify the repository name. Usage: repository:create REPOSITORY_NAME"
44
+ end
45
+ error "Please use at least 3 english word characters (a-zA-Z0-9_) without spaces for the repository name" unless repository_name =~ /^\w{3,}$/
46
+
47
+ server_configuration = get_server_configuration
48
+
49
+ # try to load the 'pg' library if repository is backed by postgres
50
+ unless server_configuration['database'] == 'derby'
51
+ error <<-MSG unless gem_available?('pg')
52
+ You should manually install the 'pg' gem if you want to create repositories backed by postgres
53
+ astroboa-cli gem does not automatically install 'pg' gem since in some environments (e.g. MAC OS X) this might require
54
+ to have a local postgres already installed, which in turn is too much if you do not care about postgres.
55
+ In *Ubuntu Linux* run first 'sudo apt-get install libpq-dev' and then run 'gem install pg'.
56
+ For MAC OS x read http://deveiate.org/code/pg/README-OS_X_rdoc.html to learn how to install the 'pg' gem.
57
+ MSG
58
+
59
+ require 'pg'
60
+ end
61
+
62
+ check_repo_existense(server_configuration, repository_name)
63
+
64
+ database_name = options[:db_name] ||= repository_name
65
+
66
+ begin
67
+ astroboa_dir = server_configuration['install_dir']
68
+ repos_dir = server_configuration['repos_dir']
69
+
70
+ # if OS is linux or [OS is mac and repos_dir is not writable by current user]
71
+ # then astroboa-cli should run with sudo in order to have the required privileges to write files
72
+ check_if_running_with_sudo if linux? || (mac_os_x? && !dir_writable?(repos_dir))
73
+
74
+ repo_dir = File.join(repos_dir, repository_name)
75
+ FileUtils.mkdir_p(repo_dir)
76
+ display "Create repository dir '#{repo_dir}': OK"
77
+
78
+ schema_dir = File.join(repo_dir, "astroboa_schemata")
79
+ FileUtils.mkdir(schema_dir)
80
+ display %(Create dir '#{schema_dir}': OK)
81
+
82
+ # This is not required any more TO BE DELETED
83
+ #internal_repo_dir = File.join(repo_dir, "repository")
84
+ #FileUtils.mkdir(internal_repo_dir)
85
+ #display %(Create dir '#{internal_repo_dir}': OK)
86
+
87
+ # create postgres database
88
+ unless server_configuration['database'] == 'derby'
89
+ begin
90
+ create_postgresql_db(server_configuration, database_name)
91
+ rescue
92
+ FileUtils.rm_r repo_dir, :secure=>true
93
+ error "Removing #{repo_dir} and Exiting..."
94
+ end
95
+ end
96
+
97
+ # config jcr repository (create repository.xml)
98
+ configure_jcr_repo(server_configuration, astroboa_dir, repo_dir, repository_name, database_name)
99
+
100
+ # config astroboa repository (create configuration in repositories-conf.xml)
101
+ configure_astroboa_repo(astroboa_dir,repos_dir, repo_dir, repository_name)
102
+
103
+ # save repo configuration into astroboa server config file
104
+ add_repo_conf_to_server_conf(server_configuration, repository_name)
105
+
106
+ rescue Exception => e
107
+ display %(An error has occured \n The error is: '#{e.to_s}' \n The error trace is: \n #{e.backtrace.join("\n")})
108
+ display %(Repository configuration, the db and the related directories will be removed)
109
+ unconfigure_astroboa_repo(repos_dir, repository_name)
110
+ FileUtils.rm_r repo_dir, :secure=>true
111
+ display "Remove #{repo_dir} : OK"
112
+ drop_postgresql_db(server_configuration, database_name) if server_configuration['database'] =~ /postgres/
113
+ error 'Exiting...'
114
+ end
115
+
116
+ end
117
+
118
+
119
+ # repository:delete REPOSITORY_NAME
120
+ #
121
+ # Unconfigures and deletes an existing astroboa repository named 'REPOSITORY_NAME'.
122
+ # BE VERY CAREFUL with this operation. All Repository configuration and data will be permanently lost.
123
+ # You are adviced to use the repository:backup command before deleting a repository in the case that you would like to recover it back.
124
+ # If you just want to disable the repository use the repository:disable / repository:enable commands
125
+ # The 'REPOSITORY_NAME' is the internal repository name.
126
+ # To find which repositories are available and see their internal names use the repository:list command
127
+ #
128
+ # -f, --force # Use this option if you wish to enforce the removal of the 'identities' repository # You must have already removed all other repositories before enforcing the 'identities' removal
129
+ #
130
+ def delete
131
+ if repository_name = args.shift
132
+ repository_name = repository_name.strip
133
+ else
134
+ error "Please specify the repository name. Usage: repository:delete REPOSITORY_NAME"
135
+ end
136
+
137
+ server_configuration = get_server_configuration
138
+
139
+ repos_dir = server_configuration["repos_dir"]
140
+
141
+ # if OS is linux or [OS is mac and repos_dir is not writable by current user]
142
+ # then astroboa-cli should run with sudo in order to have the required privileges to remove files
143
+ check_if_running_with_sudo if linux? || (mac_os_x? && !dir_writable?(repos_dir))
144
+
145
+ # check if repo exists
146
+ repo_dir = File.join(repos_dir, repository_name)
147
+ error "Repository #{repository_name} does not exist in directory #{repo_dir}" unless File.exists? repo_dir
148
+ error %(Repository #{repository_name} does not exist in astroboa server configuration file "#{get_server_conf_file}") unless repository_in_server_config?(server_configuration, repository_name)
149
+
150
+ # we treat 'identities' repo very carefully
151
+ if repository_name == 'identities'
152
+ error "You must remove all other repositories before removing the 'identities' repository" if server_configuration['repositories'].keys.length > 1
153
+ error "Use the '--force' option if you want to remove the identities repository" unless options[:force]
154
+ end
155
+
156
+ # first stop serving the repository
157
+ unconfigure_astroboa_repo(repos_dir, repository_name)
158
+
159
+ # remove dir with indexes, blobs and schemas
160
+ FileUtils.rm_r repo_dir, :secure=>true
161
+ display "Remove #{repo_dir} : OK"
162
+
163
+ # remove repo conf from astroboa server configuration
164
+ delete_repo_conf_from_server_conf(server_configuration, repository_name)
165
+
166
+ # remove the repo database, we leave it last so that everything else has been removed in the case something goes wrong
167
+ # with db removal
168
+ begin
169
+ drop_postgresql_db(server_configuration, database_name) if server_configuration['database'] =~ /postgres/
170
+ rescue
171
+ display %(The repository has been disabled and all configuration and related directories have been removed except the database.)
172
+ display %(It is safe to manually remove the database)
173
+ end
174
+
175
+
176
+ end
177
+
178
+
179
+ def disable
180
+
181
+ end
182
+
183
+
184
+ def enable
185
+
186
+ end
187
+
188
+ # repository:list REPOSITORY_NAME
189
+ #
190
+ # Lists the available repositories. If the name of an existing repository is provided it displays information about the repository.
191
+ #
192
+ def list
193
+ server_configuration = get_server_configuration
194
+ repos_dir = server_configuration["repos_dir"]
195
+
196
+ if repository_name = args.shift
197
+ repository_name = repository_name.strip
198
+
199
+ # check if repo exists
200
+ error %(Repository #{repository_name} does not exist") unless repository_in_server_config?(server_configuration, repository_name)
201
+ repo = server_configuration['repositories'][repository_name]
202
+ display "Repository Name: #{repo['id']}"
203
+ repo['localized_labels'].split(',').each do |localized_label|
204
+ locale, label = localized_label.split(':')
205
+ display "Label for locale '#{locale}': #{label}"
206
+ end
207
+ display "Authentication Token Timeout: #{repo['authenticationTokenTimeout']}"
208
+ display "Domain of Generated URLs: #{repo['serverAliasURL']}"
209
+ display "REST API Base Path: #{repo['restfulApiBasePath']}"
210
+ else
211
+ server_configuration['repositories'].each do |repo_name, repo_conf|
212
+ display "Repository Name: #{repo_name}"
213
+ end
214
+ end
215
+
216
+ end
217
+
218
+ def backup
219
+
220
+ end
221
+
222
+
223
+ def populate
224
+
225
+ end
226
+
227
+ private
228
+
229
+ def configure_jcr_repo(server_configuration, astroboa_dir, repo_dir, repository_name, database_name)
230
+ database = server_configuration['database']
231
+ database_admin = server_configuration['database_admin']
232
+ database_admin_password = server_configuration['database_admin_password']
233
+ database_server = server_configuration['database_server']
234
+
235
+ datasource_user = database_admin
236
+ datasource_password = database_admin_password
237
+
238
+ datasource_driver = 'org.postgresql.Driver'
239
+ datasource_url = "jdbc:postgresql://#{database_server}:5432/#{database_name}"
240
+ datasource_databaseType = 'postgresql'
241
+ persistence_manager_class = 'org.apache.jackrabbit.core.persistence.pool.PostgreSQLPersistenceManager'
242
+
243
+ if database == 'derby'
244
+ derby_db_file = File.join(repo_dir, "#{database_name}_derby.db")
245
+ datasource_driver = 'org.apache.derby.jdbc.AutoloadedDriver'
246
+ datasource_url = "jdbc:derby:directory:#{derby_db_file};create=true"
247
+ datasource_databaseType = 'derby'
248
+ persistence_manager_class = 'org.apache.jackrabbit.core.persistence.pool.DerbyPersistenceManager'
249
+ end
250
+
251
+ repository_template = File.join(astroboa_dir, "astroboa-setup-templates", "repository-template.xml")
252
+ jcr_repo_conf = File.join(repo_dir, "repository.xml")
253
+ context = {
254
+ :datasource_driver => datasource_driver,
255
+ :datasource_url => datasource_url,
256
+ :datasource_databaseType => datasource_databaseType,
257
+ :datasource_user => datasource_user,
258
+ :datasource_password => datasource_password,
259
+ :persistence_manager_class => persistence_manager_class
260
+ }
261
+ render_template_to_file(repository_template, context, jcr_repo_conf)
262
+ display "Generating jcr repository configuration in #{jcr_repo_conf} : OK"
263
+ end
264
+
265
+
266
+ def configure_astroboa_repo(astroboa_dir,repos_dir, repo_dir, repository_name)
267
+ repo_domain_name = options[:domain_name] ||= 'localhost:8080'
268
+ api_base_path = options[:api_base_path] ||= '/resource-api'
269
+ localized_labels = options[:localized_labels] ||= "en:#{repository_name}"
270
+ localized_labels_map = {}
271
+ localized_labels.split(',').each {|loc_lab| loc_lab_array = loc_lab.split(':'); localized_labels_map[loc_lab_array[0]] = loc_lab_array[1]}
272
+
273
+ # repos config is kept in the repositories root directory
274
+ astroboa_repos_config = File.join(repos_dir, "repositories-conf.xml")
275
+ conf_exists = true
276
+ unless File.exists? astroboa_repos_config
277
+ conf_exists = false
278
+ display %(Configuration file #{astroboa_repos_config} does not exist. Copying from templates...)
279
+ astroboa_repos_config_template = File.join(astroboa_dir, "astroboa-setup-templates", "repositories-conf.xml")
280
+ FileUtils.cp astroboa_repos_config_template, repos_dir
281
+ display %(Copy #{astroboa_repos_config_template} to #{repos_dir} : OK)
282
+ end
283
+
284
+ repo_conf = nil
285
+ File.open(astroboa_repos_config, 'r') do |f|
286
+ repo_conf = Nokogiri::XML(f) do |config|
287
+ config.noblanks
288
+ end
289
+ end
290
+
291
+ repository = Nokogiri::XML::Node.new "repository", repo_conf
292
+ repository['id'] = repository_name
293
+ repository['repository-home-directory'] = repo_dir
294
+ repository['authenticationTokenTimeout'] = "2880"
295
+ repository['serverAliasURL'] = "http://#{repo_domain_name}"
296
+ repository['restfulApiBasePath'] = api_base_path
297
+ # All repositories use the 'identities' repository as a central store for user and app identities, i.e. for storing authentication / authorization data
298
+ # The central 'identities' repository stores its own authentication / authorization data
299
+ repository['identity-store-repository-id'] = 'identities' unless repository_name == 'identities'
300
+
301
+ repo_conf.root << repository
302
+
303
+ # delete namespace of repository node so that it will be serialized as
304
+ # <repository> instead of <ns:repository> which is not recognized by astroboa SAX parser
305
+ repository.namespace = nil
306
+
307
+ localization = Nokogiri::XML::Node.new "localization", repo_conf
308
+ localized_labels_map.each do |locale, label|
309
+ label_node = Nokogiri::XML::Node.new "label", repo_conf
310
+ label_node['xml:lang'] = locale
311
+ label_node.content = label
312
+ localization << label_node
313
+ end
314
+
315
+ repository << localization
316
+
317
+ security = Nokogiri::XML::Node.new 'security', repo_conf
318
+
319
+ permanentUserKeyList = Nokogiri::XML::Node.new 'permanentUserKeyList', repo_conf
320
+ permanentUserKey = Nokogiri::XML::Node.new 'permanentUserKey', repo_conf
321
+ permanentUserKey['userid'] = 'anonymous,SYSTEM'
322
+ permanentUserKey['key'] = 'permanentUserKey'
323
+ permanentUserKeyList << permanentUserKey
324
+ security << permanentUserKeyList
325
+
326
+ secretUserKeyList = Nokogiri::XML::Node.new 'secretUserKeyList', repo_conf
327
+ administratorSecretKey = Nokogiri::XML::Node.new 'administratorSecretKey', repo_conf
328
+ administratorSecretKey['userid'] = 'SYSTEM'
329
+ administratorSecretKey['key'] = 'betaconcept'
330
+ secretUserKeyList << administratorSecretKey
331
+ security << secretUserKeyList
332
+
333
+ repository << security
334
+
335
+ jcrCache = Nokogiri::XML::Node.new 'jcrCache', repo_conf
336
+ jcrCache['maxMemory'] = '384'
337
+ jcrCache['maxMemoryPerCache'] = '32'
338
+ jcrCache['minMemoryPerCache'] = '256'
339
+
340
+ repository << jcrCache
341
+
342
+ strip_text_nodes(repo_conf)
343
+ new_astroboa_repos_config = "#{astroboa_repos_config}.new"
344
+ File.open(new_astroboa_repos_config, 'w') do |f|
345
+ repo_conf.write_xml_to(f, :indent => 1, :indent_text => "\t", :encoding => 'UTF-8')
346
+ end
347
+
348
+ # save old config file
349
+ if conf_exists
350
+ current_date = DateTime.now().strftime('%Y-%m-%dT%H.%M')
351
+ FileUtils.cp astroboa_repos_config, "#{astroboa_repos_config}.#{current_date}"
352
+ display %(Save previous repositories configuration file to #{astroboa_repos_config}.#{current_date} : OK)
353
+ end
354
+
355
+ FileUtils.mv new_astroboa_repos_config, astroboa_repos_config
356
+ display %(Created new repositories configuration file #{astroboa_repos_config})
357
+ end
358
+
359
+
360
+ def unconfigure_astroboa_repo(repos_dir, repository_name)
361
+ display "Removing configuration of repository #{repository_name} from repositories configuration file..."
362
+ astroboa_repos_config = File.join(repos_dir, "repositories-conf.xml")
363
+ if File.exists? astroboa_repos_config
364
+ repo_conf = nil
365
+ File.open(astroboa_repos_config, 'r') do |f|
366
+ repo_conf = Nokogiri::XML(f) do |config|
367
+ config.noblanks
368
+ end
369
+ end
370
+
371
+ repo_nodes = repo_conf.xpath(%(//repository[@id="#{repository_name}"]))
372
+
373
+ if !repo_nodes.empty?
374
+ repo_nodes.remove
375
+ strip_text_nodes(repo_conf)
376
+ new_astroboa_repos_config = "#{astroboa_repos_config}.new"
377
+ File.open(new_astroboa_repos_config, 'w') do |f|
378
+ repo_conf.write_xml_to(f, :indent => 1, :indent_text => "\t", :encoding => 'UTF-8')
379
+ end
380
+
381
+ current_date = DateTime.now().strftime('%Y-%m-%dT%H.%M')
382
+ FileUtils.cp astroboa_repos_config, "#{astroboa_repos_config}.#{current_date}"
383
+ display %(Save previous repositories configuration file to #{astroboa_repos_config}.#{current_date} : OK)
384
+
385
+ FileUtils.mv new_astroboa_repos_config, astroboa_repos_config
386
+ display %(Unconfigure repository #{repository_name} : OK)
387
+ else
388
+ display "WARNING: configuration settings for repository '#{repository_name}' do not exist in repositories configuration file: '#{astroboa_repos_config}'"
389
+ display "You may ignore the above warning if you tried to create a new repo and an error has occured before its creation."
390
+ end
391
+ else
392
+ error "cannot find repositories configuration file: '#{astroboa_repos_config}'"
393
+ end
394
+ end
395
+
396
+
397
+ # remove leading and trailing white space from XML Document text nodes
398
+ def strip_text_nodes(xml_doc)
399
+ xml_doc.traverse do |node|
400
+ if node.text?
401
+ node.content = node.content.strip
402
+ end
403
+ end
404
+ end
405
+
406
+
407
+ def add_repo_conf_to_server_conf(server_configuration, repository_name)
408
+ repo_domain_name = options[:domain_name] ||= 'localhost:8080'
409
+ api_base_path = options[:api_base_path] ||= '/resource-api'
410
+ localized_labels = options[:localized_labels] ||= "en:#{repository_name}"
411
+
412
+ server_configuration['repositories'] ||= {}
413
+ repository = {}
414
+ repository['id'] = repository_name
415
+ repository['authenticationTokenTimeout'] = '2880'
416
+ repository['serverAliasURL'] = "http://#{repo_domain_name}"
417
+ repository['restfulApiBasePath'] = api_base_path
418
+ repository['localized_labels'] = localized_labels
419
+ server_configuration['repositories'][repository_name] = repository
420
+ save_server_configuration(server_configuration)
421
+ display "Add repository configuration to server configuration file '#{get_server_conf_file}' : OK"
422
+ end
423
+
424
+
425
+ def delete_repo_conf_from_server_conf(server_configuration, repository_name)
426
+ if repository_in_server_config?(server_configuration, repository_name)
427
+ server_configuration['repositories'].delete(repository_name)
428
+ save_server_configuration(server_configuration)
429
+ display "Remove configuration settings for repository '#{repository_name}' from server settings file : OK"
430
+ else
431
+ display "No configuration found in server configuration file for repository '#{repository_name}'"
432
+ end
433
+ end
434
+
435
+ def check_repo_existense(server_configuration, repository_name)
436
+ repos_dir = server_configuration['repos_dir']
437
+ repo_dir = File.join(repos_dir, repository_name)
438
+ error "Repository #{repository_name} already exists in directory #{repo_dir}" if File.exists? repo_dir
439
+ error "Creation failed. A configuration for repository '#{repository_name}' exists in repositories configuration file (#{File.join(repos_dir, 'repositories-conf.xml')})" if repository_in_repos_config? repos_dir, repository_name
440
+ error "Creation failed. A configuration for repository '#{repository_name}' exists in server configuration file (#{get_server_conf_file})" if repository_in_server_config?(server_configuration, repository_name)
441
+ end
442
+
443
+ end # class Repository
@@ -0,0 +1,651 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'astroboa-cli/command/base'
4
+ require 'astroboa-cli/command/repository'
5
+ require 'fileutils'
6
+ require 'rbconfig'
7
+ require 'progressbar'
8
+ require 'net/http'
9
+ require 'uri'
10
+ require 'yaml'
11
+
12
+ # install and setup astroboa server
13
+ #
14
+ class AstroboaCLI::Command::Server < AstroboaCLI::Command::Base
15
+
16
+ # server:install
17
+ #
18
+ # Installs and setups astroboa server.
19
+ # Use the install command only for the initial installation. If you want to upgrade see 'astroboa-cli help server:upgrade'
20
+ # Before you run the install command check the following requirements:
21
+ # + You should have already installed java 1.6 and ruby 1.9.x
22
+ # + You are running this command from ruby version 1.9.x or later
23
+ # + You should have the unzip command. It is required for unzipping the downloaded packages
24
+ # + If you choose a database other than derby then the database should be already installed and running and you should know the db admin account password
25
+ #
26
+ # -i, --install_dir INSTALLATION_DIRECTORY # The full path to the directory into which to install astroboa # Default is '/opt/astroboa' in linux and '$HOME/astroboa' in mac os x and windows
27
+ # -r, --repo_dir REPOSITORIES_DIRECTORY # The full path of the directory that will contain the repositories configuration and data # Default is $installation_dir/repositories
28
+ # -d, --database DATABASE_VENDOR # Select which database to use for data persistense # Supported databases are: derby, postgres-8.2, postgres-8.3, postgres-8.4, postgres-9.0, postgres-9.1 # Default is derby
29
+ # -s, --database_server DATABASE_SERVER_IP # Specify the database server ip or FQDN (e.g 192.168.1.100 or postgres.localdomain.vpn) # Default is localhost # Not required if db is derby (it will be ignored)
30
+ # -u, --database_admin DB_ADMIN_USER # The user name of the database administrator # If not specified it will default to 'postgres' for postgresql db # Not required if db is derby (it will be ignored)
31
+ # -p, --database_admin_password PASSWORD # The password of the database administrator # Defaults to empty string # Not required if database is derby (it will be ignored)
32
+ #
33
+ def install
34
+ @torquebox_download_url = 'http://www.astroboa.org/releases/astroboa/latest/torquebox-dist-2.0.3-bin.zip'
35
+ @torquebox_package = @torquebox_download_url.split("/").last
36
+
37
+ @torquebox_version_download_url = 'http://www.astroboa.org/releases/astroboa/latest/TORQUEBOX-VERSION'
38
+ @torquebox_version_file = @torquebox_version_download_url.split("/").last
39
+
40
+ @astroboa_ear_download_url = 'http://www.astroboa.org/releases/astroboa/latest/astroboa.ear'
41
+ @astroboa_ear_package = @astroboa_ear_download_url.split("/").last
42
+
43
+ @astroboa_setup_templates_download_url = 'http://www.astroboa.org/releases/astroboa/latest/astroboa-setup-templates.zip'
44
+ @astroboa_setup_templates_package = @astroboa_setup_templates_download_url.split("/").last
45
+
46
+ @schemas_download_url = 'http://www.astroboa.org/releases/astroboa/latest/schemas.zip'
47
+ @schemas_package = @schemas_download_url.split("/").last
48
+
49
+ @astroboa_version_download_url = 'http://www.astroboa.org/releases/astroboa/latest/ASTROBOA-VERSION'
50
+ @astroboa_version_file = @astroboa_version_download_url.split("/").last
51
+
52
+ @install_dir = options[:install_dir] ||= mac_os_x? || windows? ? File.join(Dir.home, 'astroboa') : '/opt/astroboa'
53
+ @install_dir = File.expand_path @install_dir # if provided path was not absolute expand it
54
+ @repo_dir = options[:repo_dir] ||= File.join(@install_dir, "repositories")
55
+ @repo_dir = File.expand_path @repo_dir # if provided path was not absolute expand it
56
+ display <<-MSG.gsub(/^ {4}/, '')
57
+ Starting astroboa server installation
58
+ Server will be installed in: #{@install_dir}
59
+ Repository Data and config will be stored in: #{@repo_dir}
60
+ MSG
61
+
62
+ @database = options[:database] ||= 'derby'
63
+
64
+ db_error_message =<<-MSG.gsub(/^ {4}/, '')
65
+ The selected database '#{@database}' is not supported.
66
+ Supported databases are: derby, postgres-8.2, postgres-8.3, postgres-8.4, postgres-9.0, postgres-9.1
67
+ MSG
68
+
69
+ error db_error_message unless %W(derby postgres-8.2 postgres-8.3 postgres-8.4 postgres-9.0 postgres-9.1).include?(@database)
70
+
71
+ if @database.split("-").first == "postgres"
72
+ @database_admin = options[:database_admin] ||= "postgres"
73
+ @database_admin_password = options[:database_admin_password] ||= ""
74
+ else
75
+ @database_admin = "sa"
76
+ @database_admin_password = ""
77
+ end
78
+ @database_server = options[:database_server] ||= "localhost"
79
+ display "repository database is '#{@database}' accessed with user: '#{@database_admin}'"
80
+ display "Database server IP or FQDN is: #{@database_server}" if @database.split("-").first == "postgres"
81
+ # check if all requirement are fulfilled before proceeding with the installation
82
+ check_installation_requirements
83
+ download_server_components
84
+ install_server_components
85
+ save_server_configuration
86
+ create_central_identity_repository
87
+ set_astroboa_owner
88
+ cleanup_installation
89
+ # export_environment_variables
90
+ end
91
+
92
+
93
+ # server:start
94
+ #
95
+ # starts astroboa server in the foreground or as a background process.
96
+ #
97
+ # If you start it as a foreground process you can stop it by using Ctrl+c
98
+ # If you start is as a background process use 'astroboa-cli server:stop' to gracefully stop astroboa
99
+ #
100
+ # It is recommented to use this command only during development and install astroboa as a service in production systems.
101
+ # If you install astroboa as a service it will be automatically started every time your system starts.
102
+ #
103
+ # To find how to install and start / stop astroboa as a service see:
104
+ # 'astroboa-cli help service:install'
105
+ # 'astroboa-cli help service:start'
106
+ # 'astroboa-cli help service:stop'
107
+ #
108
+ # -b, --background # Starts astroboa in the background. Use 'astroboa-cli server:stop' to gracefully stop it
109
+ # -j, --jvm_options JVM_OPTIONS # java options for starting the astroboa jvm
110
+ #
111
+ def start
112
+ error 'astroboa is already running' if astroboa_running?
113
+
114
+ astroboa_installed?
115
+
116
+ server_config = get_server_configuration
117
+
118
+ # Astroboa runs inside torquebox (a special version of JBOSS AS 7) which requires jruby
119
+ # Torquebox comes with the required jruby installed.
120
+ # If the env variable 'JRUBY_HOME' exists torquebox does not use ts own jruby but that pointed by the env variable
121
+ # So we unset the variable (just in case it is set) to enforce torquebox to use its own jruby
122
+ ENV.delete('JRUBY_HOME')
123
+
124
+ # set jruby opts so that jruby runs in 1.9 mode
125
+ ENV['JRUBY_OPTS'] = '--1.9'
126
+
127
+ # don't send the gemfile from the current app
128
+ ENV.delete('BUNDLE_GEMFILE')
129
+
130
+ # append java options to the environment variable
131
+ ENV['APPEND_JAVA_OPTS'] = options[:jvm_options]
132
+
133
+ command = File.join(server_config['install_dir'], 'torquebox', 'jboss', 'bin', 'standalone.sh')
134
+
135
+
136
+ jboss_log_file = File.join(server_config['install_dir'], 'torquebox', 'jboss', 'standalone', 'log', 'server.log')
137
+
138
+ # We should always run astroboa as the user that owns the astroboa installation
139
+ # otherwise problems with file permissions may be encountered.
140
+ # If the current process owner is not the astroboa owner then we check if process owner
141
+ # is the super user (i.e astroboa-cli is run with sudo).
142
+ # If process owner is super user we change the process owner
143
+ # to be the astroboa user (we can do that since process run with sudo privileges) and we run astroboa.
144
+ # If process owner is not super user we give a notice that astroboa-cli should be executed with sudo and exit.
145
+ user = ENV['USER'] if mac_os_x? || linux?
146
+ user = ENV['USERNAME'] if windows?
147
+ install_dir = server_config['install_dir']
148
+ astroboa_uid = File.stat(install_dir).uid
149
+ astroboa_user = Etc.getpwuid(astroboa_uid).name
150
+ process_uid = Process.uid
151
+ process_user = Etc.getpwuid(process_uid).name
152
+
153
+ if astroboa_uid != process_uid
154
+ display "You are running astroboa-cli as user: #{process_user} and astroboa should run as user: #{astroboa_user}"
155
+ display "We need sudo privileges in order to do this. Lets check..."
156
+ if process_uid != 0
157
+ error <<-MSG.gsub(/^ {8}/, '')
158
+ You are not running with sudo privileges. Please run astroboa-cli with sudo
159
+ If you installed ruby with rbenv you need to install 'rbenv-sudo' plugin and then run 'rbenv sudo astroboa-cli server:start'
160
+ For 'rbenv-sudo' check ruby installation instructions at https://github.com/betaconcept/astroboa-cli
161
+ MSG
162
+ else
163
+ Process::UID.change_privilege(astroboa_uid)
164
+ display "Running with sudo: OK" if user != 'root'
165
+ display "You are root: OK" if user == 'root'
166
+ end
167
+ end
168
+
169
+ if options[:background]
170
+ display "Astroboa is starting in the background..."
171
+ display "You can check the log file with 'tail -f #{jboss_log_file}'"
172
+ display "When server startup has finished access astroboa console at: http://localhost:8080/console"
173
+ exec %(#{command} > /dev/null 2>&1 &)
174
+ #exec %(#{command} &), :pgroup => true, [:in, :out, :err] => '/dev/null'
175
+ else
176
+ display "Astroboa is starting in the foreground..."
177
+ display "When server startup has finished access astroboa console at: http://localhost:8080/console"
178
+ exec %(#{command})
179
+ end
180
+
181
+ end
182
+
183
+ # server:stop
184
+ #
185
+ # stops astroboa server if it is already running.
186
+ # It is recommented to use this command only during development and install astroboa as a service in production systems.
187
+ # To find how to install and start / stop astroboa as a service see:
188
+ # 'astroboa-cli help service:install'
189
+ # 'astroboa-cli help service:start'
190
+ # 'astroboa-cli help service:stop'
191
+ #
192
+ def stop
193
+ error 'Astroboa is not running' unless astroboa_running?
194
+ server_config = get_server_configuration
195
+ jboss_cli_command = File.join(server_config['install_dir'], 'torquebox', 'jboss', 'bin', 'jboss-cli.sh')
196
+ shutdown_command = "#{jboss_cli_command} --connect --command=:shutdown"
197
+ output = `#{shutdown_command}` if mac_os_x?
198
+ output = `su - astroboa -c "#{shutdown_command}"` if linux?
199
+ command_status = $?.to_i
200
+ if command_status == 0 && output =~ /success/
201
+ display "Astroboa has been successfully stopped"
202
+ else
203
+ error "Failed to shutdown Astroboa. Message is: #{output}"
204
+ end
205
+
206
+ end
207
+
208
+ # server:check
209
+ #
210
+ # checks if astroboa server is properly installed and displays the installation paths
211
+ # It also displays if astroboa is running
212
+ #
213
+ def check
214
+ astroboa_installed?
215
+ display astroboa_running? ? 'astroboa is running' : 'astroboa is not running'
216
+ end
217
+
218
+
219
+ private
220
+
221
+ def check_installation_requirements
222
+ display "Checking installation requirements"
223
+
224
+ # if OS is linux or [OS is mac and install_dir / repo_dir is not writable by current user]
225
+ # then astroboa-cli should run with sudo in order to have the required privileges to write files
226
+ check_if_running_with_sudo if linux? || (mac_os_x? && !(dir_writable?(@install_dir) && dir_writable?(@repo_dir)))
227
+
228
+ # do not proceed if astroboa is already installed
229
+ check_if_astroboa_exists_in_install_dirs
230
+
231
+ # installation is not currently supported on windows
232
+ check_if_os_is_windows
233
+
234
+ # check if the proper version of java is installed
235
+ java_ok?
236
+
237
+ # check if unzip command is available
238
+ check_if_unzip_is_installed
239
+
240
+ # check if 'pg' gem is installed if repositories will be backed by postgres
241
+ error <<-MSG.gsub(/^ {4}/, '') if @database.split("-").first == "postgres" && !gem_available?('pg')
242
+ You should manually install the 'pg' gem if you want to create repositories backed by postgres
243
+ astroboa-cli gem does not automatically install 'pg' gem since in some environments (e.g. MAC OS X) this might require
244
+ to have a local postgres already installed, which in turn is too much if you do not care about postgres.
245
+ In *Ubuntu Linux* run first 'sudo apt-get install libpq-dev' and then run 'gem install pg'.
246
+ For MAC OS x read http://deveiate.org/code/pg/README-OS_X_rdoc.html to learn how to install the 'pg' gem.
247
+ MSG
248
+ end
249
+
250
+
251
+ def check_if_os_is_windows
252
+ message = "astroboa server installation is currently supported for linux and mac os x"
253
+ error message if RbConfig::CONFIG['host_os'] =~ /mswin|windows|cygwin/i
254
+ display "Checking if operating system is supported: OK"
255
+ end
256
+
257
+
258
+ def check_if_astroboa_exists_in_install_dirs
259
+ astroboa_error_message = "Astroboa seems to be already installed at #{@install_dir}. Delete the installation directory or specify another install path. Run 'astroboa-cli help server:upgrade' to find how to upgrade"
260
+ repositories_error_message = "Repositories already exist at #{@repo_dir}. Specify another repository path or run 'astroboa-cli help server:upgrade' to find how to upgrade"
261
+ error astroboa_error_message if File.directory? File.join(@install_dir, "torquebox")
262
+ error repositories_error_message if File.directory? File.join(@repo_dir, "identities")
263
+ display "Verifing that Astroboa is not already installed in the specified directories: OK"
264
+ end
265
+
266
+
267
+ def astroboa_installed?
268
+ server_config = get_server_configuration
269
+
270
+ problem_message = "Astroboa is not properly installed."
271
+
272
+ astroboa_ear = Dir[File.join server_config['install_dir'], "torquebox", "jboss", "standalone", "deployments", "astroboa*.ear"].pop
273
+ error "#{problem_message} Astroboa ear package is not installed" unless astroboa_ear
274
+ display "Check astroboa ear : OK"
275
+
276
+ error "#{problem_message} Astroboa identities repository is not setup" unless File.directory? File.join(server_config['repos_dir'], "identities")
277
+ display "Check Astroboa identities repository : OK"
278
+
279
+ # since the astroboa user is the same as the astroboa-cli user we can also check the environment variables
280
+ # if mac_os_x?
281
+ # error "#{problem_message} Environment variable 'ASTROBOA_HOME' is not set. Check that your .bash_profile has run and it properly exports the 'ASTROBOA_HOME' environment variable" unless ENV['ASTROBOA_HOME']
282
+ # error "#{problem_message} Environment variable 'ASTROBOA_REPOSITORIES_HOME' is not set. Check that your .bash_profile has run and it properly exports the 'ASTROBOA_REPOSITORIES_HOME' environment variable" unless ENV['ASTROBOA_REPOSITORIES_HOME']
283
+ # error "#{problem_message} Environment variable 'JBOSS_HOME' is not set. Check that your .bash_profile has run and it properly exports the 'JBOSS_HOME' environment variable" unless ENV['JBOSS_HOME']
284
+ # display "Check existence of required environment variables : OK"
285
+ #
286
+ # display "Check consistency between environment variables and Astroboa Server Settings File #{get_server_conf_file} ", false
287
+ # error "#{problem_message} Missmatch of Astroboa installation dir in environmet variable 'ASTROBOA_HOME' (#{ENV['ASTROBOA_HOME']}) and server settings (#{server_config['install_dir']})" unless server_config['install_dir'] == ENV['ASTROBOA_HOME']
288
+ # error "#{problem_message} Missmatch of repositories dir in environmet variable 'ASTROBOA_REPOSITORIES_HOME' (#{ENV['ASTROBOA_REPOSITORIES_HOME']}) and server config settings (#{server_config['repos_dir']})" unless server_config['repos_dir'] == ENV['ASTROBOA_REPOSITORIES_HOME']
289
+ # error "#{problem_message} The mandatory repository 'identities' is not configured in server settings. Use the command 'repository:create identities' to create it." unless repository_in_server_config?(server_config, 'identities')
290
+ # display ": OK"
291
+ # end
292
+
293
+ ok_message = "Astroboa installaion is ok.\nInstallation Path: #{server_config['install_dir']}\nRepository configuration and data are stored in: #{server_config['repos_dir']}"
294
+ display ok_message
295
+ end
296
+
297
+
298
+ def java_ok?
299
+ error('Please install java 6 (version 1.6.x) or java 7 (version 1.7.x) to proceed with installation') unless has_executable_with_version("java", "1\\.6|7", '-version')
300
+ end
301
+
302
+
303
+ def check_if_wget_is_installed
304
+ error('Some files need to be downloaded. Please install \'wget\' and run the installation again') unless has_executable("wget")
305
+ end
306
+
307
+
308
+ def check_if_unzip_is_installed
309
+ error('Some archives need to be unzipped. Please install \'unzip\' and run the installation again') unless has_executable("unzip")
310
+ end
311
+
312
+
313
+ def download_server_components
314
+ # create installation directory
315
+ begin
316
+ FileUtils.mkdir_p @install_dir
317
+ rescue SystemCallError => e
318
+ error "Failed to create installation directory '#{@install_dir}' \n the Error is: #{e.message}"
319
+ end
320
+
321
+ display "Dowloading astroboa server components to #{@install_dir}"
322
+
323
+ # download torquebox
324
+ download_package(@torquebox_download_url, @install_dir) unless File.size?(File.join(@install_dir, @torquebox_package)) == 173153188
325
+
326
+ # download torquebox version file
327
+ download_package(@torquebox_version_download_url, @install_dir) unless File.size?(File.join(@install_dir, @torquebox_version_file)) == 6
328
+
329
+ # download astroboa ear
330
+ download_package(@astroboa_ear_download_url, @install_dir) unless File.size?(File.join(@install_dir, @astroboa_ear_package)) == 64585240
331
+
332
+ # download astroboa version file
333
+ download_package(@astroboa_version_download_url, @install_dir) unless File.size?(File.join(@install_dir, @astroboa_version_file)) == 15
334
+
335
+ # download astroboa setup templates
336
+ download_package(@astroboa_setup_templates_download_url, @install_dir) unless File.size?(File.join(@install_dir, @astroboa_setup_templates_package)) == 11030750
337
+
338
+ # download astroboa schemas
339
+ download_package(@schemas_download_url, @install_dir) unless File.size?(File.join(@install_dir, @schemas_download_url)) == 28426
340
+ end
341
+
342
+
343
+ def download_package_with_wget(package_url, install_dir)
344
+ command = %(bash -c 'wget -c --directory-prefix=#{install_dir} #{package_url} 2>>#{log_file}')
345
+ package = package_url.split('/').last
346
+ log.info "Downloading #{package} with command: #{command}"
347
+ display "Downloading #{package}"
348
+ error "Failed to download package '#{package}'. Check logfile #{log_file}" unless process_os_command command
349
+ end
350
+
351
+
352
+ def download_package(package_url, install_dir)
353
+ package_uri = URI.parse package_url
354
+ package = package_url.split('/').last
355
+ file = File.join install_dir, package
356
+ display "Downloading #{package} from #{package_uri.host} to #{file}"
357
+
358
+ Net::HTTP.start package_uri.host, package_uri.port do |http|
359
+ bytesDownloaded = 0
360
+ http.request Net::HTTP::Get.new(package_uri.path) do |response|
361
+ pBar = ProgressBar.new package, 100
362
+ size = response.content_length
363
+ File.open(file,'w') do |file|
364
+ response.read_body do |segment|
365
+ bytesDownloaded += segment.length
366
+ if bytesDownloaded != 0
367
+ percentDownloaded = (bytesDownloaded * 100) / size
368
+ pBar.set(percentDownloaded)
369
+ end
370
+ file.write(segment)
371
+ end
372
+ pBar.finish
373
+ end
374
+ end
375
+ end
376
+
377
+ log.info "#{package} downloaded successfully"
378
+ end
379
+
380
+
381
+ def install_server_components
382
+ create_astroboa_user
383
+ display "Installing server components to #{@install_dir}"
384
+ install_torquebox
385
+ install_astroboa
386
+ end
387
+
388
+
389
+ def create_astroboa_user
390
+ # in mac os x we do not create a separate user
391
+ user = 'astroboa'
392
+ if linux?
393
+ display "Adding usergroup and user '#{user}'"
394
+ command = "groupadd -f #{user} 2>>#{log_file}"
395
+ error "Failed to create usergroup astroboa. Check logfile #{log_file}" unless process_os_command command
396
+ command = "useradd -m -g #{user} #{user} 2>>#{log_file}"
397
+ error "Failed to create user astroboa. Check logfile #{log_file}" unless process_os_command command
398
+ end
399
+ end
400
+
401
+
402
+ def install_torquebox
403
+ unzip_torquebox
404
+ # we cannot use the ruby lib to unzip since it does not preserve file permissions
405
+ #unzip_file(File.join(@install_dir, @torquebox_package), @install_dir)
406
+ create_torquebox_symbolic_link
407
+
408
+ # may be that we do not need this any more
409
+ # add_torquebox_env_settings
410
+ end
411
+
412
+
413
+ def unzip_torquebox
414
+ command = %(bash -c 'cd #{@install_dir} && #{extract_archive_command @torquebox_package} #{File.join(@install_dir, @torquebox_package)} 2>>#{log_file}')
415
+ log.info "Installing torquebox with command: #{command}"
416
+ error "Failed to install torquebox" unless process_os_command command
417
+ end
418
+
419
+
420
+ def create_torquebox_symbolic_link
421
+ # create a symbolic link from the versioned directory to which torquebox was extracted (e.g. torquebox-2.0.cr1) to just 'torquebox'
422
+ # we need this in order to create the required export paths once instead of recreating them each time torquebox is upgrated
423
+ begin
424
+ torquebox_dir = Dir["#{@install_dir}/torquebox*/"].pop
425
+ display %(Adding symbolic link from #{torquebox_dir} to #{File.join(@install_dir, "torquebox")})
426
+ FileUtils.ln_s "#{torquebox_dir}", File.join(@install_dir, "torquebox")
427
+ rescue SystemCallError => e
428
+ error %(Failed to create symbolic link from '#{File.join(@install_dir, torquebox_dir)}' to '#{File.join(@install_dir, "torquebox")}' \n the Error is: #{e.message})
429
+ end
430
+ end
431
+
432
+
433
+ def add_torquebox_env_settings
434
+ # add required environment settings to .bash_profile
435
+ user_dir = File.expand_path("~astroboa") if linux?
436
+ user_dir = ENV["HOME"] if mac_os_x?
437
+
438
+ display "Adding required environment settings in #{user_dir}/.bash_profile"
439
+ bash_profile_path = File.join(user_dir, ".bash_profile")
440
+ settings_start_here_comment = '# ASTROBOA REQUIRED PATHS CONFIGURATION STARTS HERE'
441
+ settings_end_here_comment = '# ASTROBOA REQUIRED PATHS CONFIGURATION ENDS HERE'
442
+ # remove any previous settings
443
+ delete_file_content_between_regex(bash_profile_path, settings_start_here_comment, settings_end_here_comment) if File.exists? bash_profile_path
444
+ # write the new settings
445
+ File.open(bash_profile_path, 'a+') do |f|
446
+ env_settings =<<SETTINGS
447
+
448
+ #{settings_start_here_comment}
449
+ export ASTROBOA_HOME=#{@install_dir}
450
+ export ASTROBOA_REPOSITORIES_HOME=#{@repo_dir}
451
+ export TORQUEBOX_HOME=$ASTROBOA_HOME/torquebox
452
+ export JBOSS_HOME=$TORQUEBOX_HOME/jboss
453
+ #{"export PATH=$JRUBY_HOME/bin:$PATH" if linux?}
454
+ #{settings_end_here_comment}
455
+ SETTINGS
456
+
457
+ f.write env_settings
458
+ end
459
+ end
460
+
461
+
462
+ def unzip_schemas
463
+ unzip_file(File.join(@install_dir, @schemas_package), @install_dir)
464
+ end
465
+
466
+
467
+ def install_astroboa
468
+ # unzip the templates first
469
+ unzip_file(File.join(@install_dir, @astroboa_setup_templates_package), @install_dir)
470
+
471
+ # unzip astroboa schemas that are used for user schema validation
472
+ unzip_schemas
473
+
474
+ jboss_dir = File.join(@install_dir, "torquebox", "jboss")
475
+ jboss_modules_dir = File.join(jboss_dir, "modules")
476
+ astroboa_setup_templates_dir = File.join(@install_dir, "astroboa-setup-templates")
477
+
478
+ create_repo_dir
479
+
480
+ install_astroboa_ear(jboss_dir)
481
+
482
+ install_jdbc_modules(astroboa_setup_templates_dir, jboss_modules_dir)
483
+
484
+ install_spring_modules(astroboa_setup_templates_dir, jboss_modules_dir)
485
+
486
+ install_jboss_runtime_config(astroboa_setup_templates_dir, jboss_dir)
487
+
488
+ install_jboss_config(astroboa_setup_templates_dir, jboss_dir)
489
+
490
+ end
491
+
492
+
493
+ def create_repo_dir
494
+ # create directory for repository data and astroboa repositories configuration file
495
+ begin
496
+ FileUtils.mkdir_p @repo_dir
497
+ display "Creating Repositories Directory: OK"
498
+ rescue SystemCallError => e
499
+ error "Failed to create repositories directory '#{@repo_dir}' \n the Error is: #{e.message}"
500
+ end
501
+ end
502
+
503
+
504
+ def install_astroboa_ear(jboss_dir)
505
+ FileUtils.cp File.join(@install_dir, @astroboa_ear_package), File.join(jboss_dir, "standalone", "deployments")
506
+ display "Copying astroboa ear package into jboss deployments: OK"
507
+ end
508
+
509
+
510
+ def install_jdbc_modules(astroboa_setup_templates_dir, jboss_modules_dir)
511
+ # copy both derby and postgres jdbc driver module
512
+ # This is required since both derby and postgres modules have been specified as dependencies of astroboa.ear module
513
+ FileUtils.cp_r File.join(astroboa_setup_templates_dir, "jdbc-drivers", "derby", "org"), jboss_modules_dir
514
+ display "Copying derby jdbc driver module into jboss modules #{("(derby module is installed even if postgres has been selected)" unless @database == 'derby')}: OK"
515
+ # copy postgres driver
516
+ # if postgres has been specified in options then install the drivers for the specified version
517
+ # else install drivers for postgres 9.1
518
+ postgres_db = @database
519
+ postgres_db = 'postgres-9.1' if @database == 'derby'
520
+ FileUtils.cp_r File.join(astroboa_setup_templates_dir, "jdbc-drivers", postgres_db, "org"), jboss_modules_dir
521
+ display %(Copying #{postgres_db} jdbc driver module into jboss modules #{("(postgres drivers are copied even if derby has been selected)" if @database == 'derby')}: OK)
522
+ end
523
+
524
+
525
+ def install_spring_modules(astroboa_setup_templates_dir, jboss_modules_dir)
526
+ # copy spring and snowdrop modules to jboss modules
527
+ FileUtils.cp_r File.join(astroboa_setup_templates_dir, "jboss-modules", "org"), jboss_modules_dir
528
+ display "Copying spring and snowdrop modules into jboss modules: OK"
529
+ end
530
+
531
+
532
+ def install_jboss_runtime_config(astroboa_setup_templates_dir, jboss_dir)
533
+ # preserve original jboss runtime config and copy customized runtime config into jboss bin directory
534
+ original_runtime_config = File.join(jboss_dir, "bin", "standalone.conf")
535
+ FileUtils.cp original_runtime_config, "#{original_runtime_config}.original"
536
+ FileUtils.cp File.join(astroboa_setup_templates_dir, "standalone.conf"), original_runtime_config
537
+ display "Copying jboss runtime config into jboss bin: OK"
538
+ end
539
+
540
+
541
+ def install_jboss_config(astroboa_setup_templates_dir, jboss_dir)
542
+ # create jboss config from template and write it to jboss standalone configuration directory, preserving the original file
543
+ original_jboss_config = File.join(jboss_dir, "standalone", "configuration", "standalone.xml")
544
+ FileUtils.cp original_jboss_config, "#{original_jboss_config}.original"
545
+ jboss_config_template = File.join(astroboa_setup_templates_dir, "standalone.xml")
546
+ context = {:astroboa_config_dir => @repo_dir}
547
+ render_template_to_file(jboss_config_template, context, original_jboss_config)
548
+ display "Generating and copying jboss config into jboss standalone configuration directory: OK"
549
+ end
550
+
551
+
552
+ # currently not used - consider to remove
553
+ def create_pgpass_file
554
+ if @database.split("-").first == "postgres"
555
+ pgpass_file = File.expand_path(File.join("~",".pgpass"))
556
+ pgpass_file = File.expand_path(File.join("~astroboa",".pgpass")) unless RbConfig::CONFIG['host_os'] =~ /darwin/i
557
+
558
+ pgpass_config = "localhost:5432:*:#{@database_admin}:#{@database_admin_password}"
559
+
560
+ File.open(pgpass_file,"w") do |f|
561
+ f.write(pgpass_config)
562
+ end
563
+ display "The file '#{pgpass_file}' has been created to give astroboa user permission to run postgres admin commands"
564
+ end
565
+ end
566
+
567
+
568
+ def save_server_configuration
569
+ config_file = File.expand_path(File.join('~', '.astroboa-conf.yml'))
570
+ unless RbConfig::CONFIG['host_os'] =~ /darwin/i
571
+ config_dir = File.join(File::SEPARATOR, 'etc', 'astroboa')
572
+ FileUtils.mkdir_p config_dir
573
+ config_file = File.join(config_dir, 'astroboa-conf.yml')
574
+ end
575
+ server_config = {}
576
+ server_config['install_dir'] = @install_dir
577
+ server_config['repos_dir'] = @repo_dir
578
+ server_config['database'] = @database
579
+ server_config['database_admin'] = @database_admin
580
+ server_config['database_admin_password'] = @database_admin_password
581
+ server_config['database_server'] = @database_server
582
+
583
+ File.open(config_file,"w") do |f|
584
+ f.write(YAML.dump(server_config))
585
+ end
586
+ display "The server configuration have been added to configuration file '#{config_file}'"
587
+ end
588
+
589
+
590
+ def create_central_identity_repository
591
+ repo_name = 'identities'
592
+ repo_config = {
593
+ 'localized_labels' => 'en:User and App Identities,el:Ταυτότητες Χρηστών και Εφαρμογών'
594
+ }
595
+ AstroboaCLI::Command::Repository.new([repo_name], repo_config).create
596
+ display "Create Central 'Identities and Apps' Repository with name 'identities' : OK"
597
+ end
598
+
599
+ def set_astroboa_owner
600
+ # In mac os x astroboa is installed and run under the ownership of the user that runs the installation command.
601
+ # If installation is done with sudo then we need to change the ownership of astroboa installation to the current user.
602
+ if mac_os_x? && running_with_sudo?
603
+ user = ENV['USER']
604
+ FileUtils.chown_R(user, nil, @install_dir)
605
+ display "Change (recursively) user owner of #{@install_dir} to #{user}: OK"
606
+
607
+ # if repositories dir is outside the main install dir then we should change the ownership there too
608
+ unless File.join(@install_dir, 'repositories') == @repo_dir
609
+ FileUtils.chown_R(user, nil, @repo_dir)
610
+ display "Change (recursively) user owner of #{@repo_dir} to #{user}: OK"
611
+ end
612
+
613
+ end
614
+
615
+ # In linux a special user 'astroboa' and group 'astroboa' is created for owning a running astroboa.
616
+ # So we need to change the ownership of the installation dir and the repositories dir to belong to user 'astroboa' and group 'astroboa'
617
+ if linux?
618
+
619
+ FileUtils.chown_R('astroboa', 'astroboa', @install_dir)
620
+ display "Change (recursively) user and group owner of #{@install_dir} to 'astroboa': OK"
621
+
622
+ # if repositories dir is outside the main install dir then we should change the ownership there too
623
+ unless File.join(@install_dir, 'repositories') == @repo_dir
624
+ FileUtils.chown_R('astroboa', 'astroboa', @repo_dir)
625
+ display "Change (recursively) user and group owner of #{@repo_dir} to 'astroboa': OK"
626
+ end
627
+
628
+ end
629
+ end
630
+
631
+ def cleanup_installation
632
+ display "Cleaning not required Installation packages..."
633
+ FileUtils.rm File.join(@install_dir, @torquebox_package)
634
+ display "Removed torquebox package"
635
+
636
+ FileUtils.rm File.join(@install_dir, @astroboa_ear_package)
637
+ display "Removed astroboa ear package"
638
+
639
+ FileUtils.rm File.join(@install_dir, @astroboa_setup_templates_package)
640
+ display "Removed setup templates package"
641
+
642
+ FileUtils.rm File.join(@install_dir, @schemas_package)
643
+ display "Removed schemas package"
644
+
645
+ display "Installation cleanup: OK"
646
+ end
647
+
648
+ end
649
+
650
+
651
+