Module: VMC::KNIFE
- Defined in:
- lib/vmc_knife/version.rb,
lib/vmc_knife/vmc_knife.rb,
lib/vmc_knife/vmc_helper.rb,
lib/vmc_knife/data_services.rb,
lib/vmc_knife/json_expander.rb
Defined Under Namespace
Modules: Cli, HELPER, JSON_EXPANDER Classes: Application, ApplicationEnvironment, ApplicationManifestApplier, DataService, DataServiceManifestApplier, Recipe, RecipesConfigurationApplier, Root, VCAPUpdateAvahiAliases, VCAPUpdateCloudControllerConfig, VCAPUpdateEtcHosts
Constant Summary collapse
- PSQL_RAW_RES_ARGS =
returns only the result raw from psql
"-P format=unaligned -P footer=off -P tuples_only=on"
Class Method Summary collapse
- .as_regexp(arg) ⇒ Object
-
.data_service_console(credentials_hash, commands_file = "", as_admin = false, exec_name = nil, return_value = false) ⇒ Object
command_files or command.
- .get_app_id(app_name) ⇒ Object
-
.get_ccdb_credentials(ccdb_yml_path = "#{ENV['CLOUD_FOUNDRY_CONFIG_PATH']}/cloud_controller.yml", db_type = 'production') ⇒ Object
Reads the cloud_controller config file for the connection parameters to ccdb.
-
.get_credentials(service_name, app_name = nil) ⇒ Object
Returns a hash of the credentials for a data-service For example for postgres: — name: dc82ca85dfef740b7841211f354068beb host: 192.168.1.6 hostname: 192.168.1.6 port: 5432 user: uafe612fbe7714af0ab04db22e199680d username: uafe612fbe7714af0ab04db22e199680d password: pd829916bfac34b3185e0f1158bf8920b node_id: postgresql_node_0 .
- .get_mongo_exec(exec_name = nil) ⇒ Object
-
.get_mongodb_base_dir(mongodb_node_yml_path = nil) ⇒ Object
Returns the path to the mongodb db files.
- .get_mongodb_node_config(mongodb_node_yml_path = "#{ENV['CLOUD_FOUNDRY_CONFIG_PATH']}/mongodb_node.yml") ⇒ Object
- .get_postgresql_node_credentials(postgresql_node_yml_path = "#{ENV['CLOUD_FOUNDRY_CONFIG_PATH']}/postgresql_node.yml") ⇒ Object
- .get_service_config_id(service_name) ⇒ Object
- .pg_connect_cmd(credentials_hash, executable = 'psql', as_admin = false, other_params = "") ⇒ Object
Class Method Details
.as_regexp(arg) ⇒ Object
175 176 177 178 179 180 181 |
# File 'lib/vmc_knife/data_services.rb', line 175 def self.as_regexp(arg) if arg != nil && arg.kind_of?(String) && !arg.strip.empty? Regexp.new(arg) elsif arg.kind_of?(Regexp) arg end end |
.data_service_console(credentials_hash, commands_file = "", as_admin = false, exec_name = nil, return_value = false) ⇒ Object
command_files or command.
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/vmc_knife/data_services.rb', line 110 def self.data_service_console(credentials_hash, commands_file="",as_admin=false,exec_name=nil,return_value=false) if credentials_hash['db'] #so far it has always been equal to 'db' # It is a mongo service #/home/ubuntu/cloudfoundry/.deployments/intalio_devbox/deploy/mongodb/bin/mongo 127.0.0.1:25003/db #-u c417f26c-6f49-4dd5-a208-216107279c7a -p 8ab08355-6509-48d5-974f-27c853b842f5 # Todo: compute the mongoshell path (?) exec_name ||= 'mongo' mongo_shell=get_mongo_exec(exec_name) if exec_name == 'mongo' db_arg = "/#{credentials_hash['db']}" elsif exec_name == 'mongodump' db_arg = "" # dump all the databases including 'admin' which contains the users. else db_arg = " --db #{credentials_hash['db']}" end cmd = "#{mongo_shell} -u #{credentials_hash['username']} -p #{credentials_hash['password']} #{'-h' if(exec_name=='mongorestore')} #{credentials_hash['hostname']}:#{credentials_hash['port']}#{db_arg}" if commands_file if exec_name == 'mongo' if File.exists? commands_file commands_file = "--eval \"`cat #{commands_file}`\"" else commands_file = "--eval \"#{commands_file}\"" end end the_cmd = "#{cmd} #{commands_file}" puts "#{the_cmd}" if VMC::Cli::Config.trace puts `#{the_cmd}` else puts "#{cmd}" if VMC::Cli::Config.trace # Replaces the current process. exec cmd end else cmd = pg_connect_cmd(credentials_hash, 'psql') if commands_file if File.exists? commands_file commands_file = "-f #{commands_file} #{PSQL_RAW_RES_ARGS}" else commands_file = "-c \"#{commands_file}\" #{PSQL_RAW_RES_ARGS}" end the_cmd = "#{cmd} #{commands_file}" puts "#{the_cmd}" if VMC::Cli::Config.trace return `#{the_cmd}` if return_value puts `#{the_cmd}` else puts "#{cmd}" if VMC::Cli::Config.trace # Replaces the current process. exec cmd end end end |
.get_app_id(app_name) ⇒ Object
31 32 33 34 35 |
# File 'lib/vmc_knife/data_services.rb', line 31 def self.get_app_id(app_name) db=get_ccdb_credentials() app_id = `psql --username #{db['username']} --dbname #{db['database']} -c \"select id from apps where name='#{app_name}'\" #{PSQL_RAW_RES_ARGS}`.strip app_id unless app_id.empty? end |
.get_ccdb_credentials(ccdb_yml_path = "#{ENV['CLOUD_FOUNDRY_CONFIG_PATH']}/cloud_controller.yml", db_type = 'production') ⇒ Object
Reads the cloud_controller config file for the connection parameters to ccdb.
15 16 17 18 19 |
# File 'lib/vmc_knife/data_services.rb', line 15 def self.get_ccdb_credentials(ccdb_yml_path="#{ENV['CLOUD_FOUNDRY_CONFIG_PATH']}/cloud_controller.yml", db_type='production') cc_conf = File.open( ccdb_yml_path ) do |yf| YAML::load( yf ) end db = cc_conf['database_environment'][db_type] db end |
.get_credentials(service_name, app_name = nil) ⇒ Object
Returns a hash of the credentials for a data-service For example for postgres:
name: dc82ca85dfef740b7841211f354068beb host: 192.168.1.6 hostname: 192.168.1.6 port: 5432 user: uafe612fbe7714af0ab04db22e199680d username: uafe612fbe7714af0ab04db22e199680d password: pd829916bfac34b3185e0f1158bf8920b node_id: postgresql_node_0
For example for mongo: hostname: 192.168.0.103 + host: 192.168.0.103 + port: 25003 + name: 266401da-6853-4657-b212-814bd6f9d844 + db: db + username: c417f26c-6f49-4dd5-a208-216107279c7a+ password: 8ab08355-6509-48d5-974f-27c853b842f5+ node_id: mongodb_node_0
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/vmc_knife/data_services.rb', line 65 def self.get_credentials(service_name, app_name=nil) db=get_ccdb_credentials() if app_name.nil? credentials_str = `psql --username #{db['username']} --dbname #{db['database']} -c \"select credentials from service_configs where alias='#{service_name}'\" #{PSQL_RAW_RES_ARGS}`.strip else app_id = get_app_id(app_name) if app_id.nil? puts "WARN: No application named #{app_name} exists at this point" if VMC::Cli::Config.trace return end service_config_id = get_service_config_id(service_name) credentials_str = `psql --username #{db['username']} --dbname #{db['database']} -c \"select credentials from service_bindings where app_id = '#{app_id}' and service_config_id='#{service_config_id}'\" #{PSQL_RAW_RES_ARGS}`.strip end res = Hash.new credentials_str.split("\n").each do | line | line =~ /([\w]*): ([\w|\.|-]*)$/ res[$1] = $2 if $2 end res end |
.get_mongo_exec(exec_name = nil) ⇒ Object
162 163 164 165 166 |
# File 'lib/vmc_knife/data_services.rb', line 162 def self.get_mongo_exec(exec_name=nil) exec_name||='mongo' mongo_bin_folder=File.dirname(get_mongodb_node_config()['mongod_path']) File.join(mongo_bin_folder,exec_name) end |
.get_mongodb_base_dir(mongodb_node_yml_path = nil) ⇒ Object
Returns the path to the mongodb db files. /var/vcap/services/mongodb/
169 170 171 172 173 |
# File 'lib/vmc_knife/data_services.rb', line 169 def self.get_mongodb_base_dir(mongodb_node_yml_path=nil) mongodb_node_yml_path||="#{ENV['CLOUD_FOUNDRY_CONFIG_PATH']}/mongodb_node.yml" db = File.open( mongodb_node_yml_path ) do |yf| YAML::load( yf ) end base_dir = db['base_dir'] end |
.get_mongodb_node_config(mongodb_node_yml_path = "#{ENV['CLOUD_FOUNDRY_CONFIG_PATH']}/mongodb_node.yml") ⇒ Object
26 27 28 29 |
# File 'lib/vmc_knife/data_services.rb', line 26 def self.get_mongodb_node_config(mongodb_node_yml_path="#{ENV['CLOUD_FOUNDRY_CONFIG_PATH']}/mongodb_node.yml") db = File.open( mongodb_node_yml_path ) do |yf| YAML::load( yf ) end db end |
.get_postgresql_node_credentials(postgresql_node_yml_path = "#{ENV['CLOUD_FOUNDRY_CONFIG_PATH']}/postgresql_node.yml") ⇒ Object
21 22 23 24 |
# File 'lib/vmc_knife/data_services.rb', line 21 def self.get_postgresql_node_credentials(postgresql_node_yml_path="#{ENV['CLOUD_FOUNDRY_CONFIG_PATH']}/postgresql_node.yml") db = File.open( postgresql_node_yml_path ) do |yf| YAML::load( yf ) end db['postgresql'] end |
.get_service_config_id(service_name) ⇒ Object
36 37 38 39 40 41 |
# File 'lib/vmc_knife/data_services.rb', line 36 def self.get_service_config_id(service_name) db=get_ccdb_credentials() #todo add the user_id service_config_id = `psql --username #{db['username']} --dbname #{db['database']} -c \"select id from service_configs where alias='#{service_name}'\" #{PSQL_RAW_RES_ARGS}`.strip service_config_id unless service_config_id.empty? end |
.pg_connect_cmd(credentials_hash, executable = 'psql', as_admin = false, other_params = "") ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/vmc_knife/data_services.rb', line 86 def self.pg_connect_cmd(credentials_hash, executable='psql',as_admin=false, other_params="") if executable =~ /vacuumlo$/ # we don't have TEMP privilegeswhich are required by vacuumlo... #"export PGPASSWORD=#{credentials_hash['password']}; #{executable} -h #{credentials_hash['hostname']} -p #{credentials_hash['port']} -U #{credentials_hash['username']} #{credentials_hash['name']}" #workaround: use the superuser in the meantime: db=get_postgresql_node_credentials() db_arg = credentials_hash['name'] return "export PGPASSWORD=#{db['pass']}; #{executable} -h #{db['host']} -p #{db['port']} -U #{db['user']} #{other_params} #{db_arg}" elsif executable =~ /pg_dump$/ db_arg = "#{other_params} #{credentials_hash['name']}" else #the other params are at the end db_arg = "--dbname=#{credentials_hash['name']} #{other_params}" end if as_admin # usually as vcap/vcap db=get_postgresql_node_credentials() "export PGPASSWORD=#{db['pass']}; #{executable} --host=#{db['host']} --port=#{db['port']} --username=#{db['user']} #{db_arg}" else "export PGPASSWORD=#{credentials_hash['password']}; #{executable} --host=#{credentials_hash['hostname']} --port=#{credentials_hash['port']} --username=#{credentials_hash['username']} #{db_arg}" end end |