def get_config(cls): okeanos_ssh_key_path = os.environ.get('OKEANOS_SSH_KEY') if not okeanos_ssh_key_path: raise ConfigError("Please set the OKEANOS_SSH_KEY with the path to your public ssh key") kamakirc_path = os.environ.get('OKEANOS_KAMAKIRC') okeanos_config = Config(kamakirc_path) # This is debian specific... for now... okeanos_config.set('global', 'ca_certs', '/etc/ssl/certs/ca-certificates.crt') cloud_name = okeanos_config.get('global', 'default_cloud') auth_url = okeanos_config.get_cloud(cloud_name, 'url') auth_token = okeanos_config.get_cloud(cloud_name, 'token') if (not cloud_name or not auth_url or not auth_token): raise ConfigError("Wrong okeanos configuration") return okeanos_config
def test_get_cloud(self): from kamaki.cli.config import Config, CLOUD_PREFIX _cnf = Config(path=self.f.name) d = dict(opt1='v1', opt2='v2') with patch('kamaki.cli.config.Config.get', return_value=d) as get: self.assertEqual('v1', _cnf.get_cloud('mycloud', 'opt1')) self.assertEqual(get.mock_calls[-1], call(CLOUD_PREFIX, 'mycloud')) self.assertRaises(KeyError, _cnf.get_cloud, 'mycloud', 'opt3') with patch('kamaki.cli.config.Config.get', return_value=0) as get: self.assertRaises(KeyError, _cnf.get_cloud, 'mycloud', 'opt1')
def test_get_cloud(self): from kamaki.cli.config import Config, CLOUD_PREFIX _cnf = Config(path=self.f.name) d = dict(opt1='v1', opt2='v2') with patch('kamaki.cli.config.Config.get', return_value=d) as get: self.assertEqual('v1', _cnf.get_cloud('mycloud', 'opt1')) self.assertEqual( get.mock_calls[-1], call(CLOUD_PREFIX, 'mycloud')) self.assertRaises(KeyError, _cnf.get_cloud, 'mycloud', 'opt3') with patch('kamaki.cli.config.Config.get', return_value=0) as get: self.assertRaises(KeyError, _cnf.get_cloud, 'mycloud', 'opt1')
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE. # # The views and conclusions contained in the software and # documentation are those of the authors and should not be # interpreted as representing official policies, either expressed # or implied, of GRNET S.A. from kamaki.cli.config import Config from kamaki.clients import astakos, cyclades, ClientError from kamaki.clients.utils import https https.patch_with_certs('/etc/ssl/certs/ca-certificates.crt') cnf = Config() CLOUD = cnf.get('global', 'default_cloud') URL = cnf.get_cloud(CLOUD, 'url') TOKEN = cnf.get_cloud(CLOUD, 'token') identity_client = astakos.CachedAstakosClient(URL, TOKEN) computeURL = identity_client.get_endpoint_url( cyclades.CycladesComputeClient.service_type) compute_client = cyclades.CycladesComputeClient(computeURL, TOKEN) volumeURL = identity_client.get_endpoint_url( cyclades.CycladesBlockStorageClient.service_type) volume_client = cyclades.CycladesBlockStorageClient(volumeURL, TOKEN) srv = compute_client.get_server_details(454001) snapshots = [snp for snp in volume_client.list_snapshots(detail=True) if ( snp['volume_id'] == srv['volumes'][0])]
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE. # # The views and conclusions contained in the software and # documentation are those of the authors and should not be # interpreted as representing official policies, either expressed # or implied, of GRNET S.A. from kamaki.cli.config import Config from kamaki.clients import astakos, cyclades, ClientError from kamaki.clients.utils import https https.patch_with_certs('/etc/ssl/certs/ca-certificates.crt') cnf = Config() CLOUD = cnf.get('global', 'default_cloud') URL = cnf.get_cloud(CLOUD, 'url') TOKEN = cnf.get_cloud(CLOUD, 'token') identity_client = astakos.CachedAstakosClient(URL, TOKEN) computeURL = identity_client.get_endpoint_url( cyclades.CycladesComputeClient.service_type) compute_client = cyclades.CycladesComputeClient(computeURL, TOKEN) img = dict(id='10b79e4d-9362-4f30-9223-3105c9a84bce') srv_name = raw_input('Server name: ') flv = compute_client.get_flavor_details(raw_input('Flavor id: ')) srv = compute_client.create_server(srv_name, flavor_id=flv['id'], image_id=img['id'])
def test_rescue_old_file(self): from kamaki.cli.config import Config content0 = list(self.config_file_content) def make_file(lines): f = NamedTemporaryFile() f.writelines(lines) f.flush() return f with make_file(content0) as f: _cnf = Config(path=f.name) self.assertEqual([], _cnf.rescue_old_file()) del _cnf content1, sample = list(content0), 'xyz_cli = XYZ_specs' content1.insert(2, '%s\n' % sample) with make_file(content1) as f: f.seek(0) _cnf = Config(path=f.name) self.assertEqual( sorted(['global.%s' % sample]), sorted(_cnf.rescue_old_file())) del _cnf content2, sample = list(content0), 'http://www.example2.org' content2.insert(2, 'url = %s\n' % sample) err = StringIO() with make_file(content2) as f: _cnf = Config(path=f.name) self.assertEqual([], _cnf.rescue_old_file(err=err)) self.assertEqual( '... rescue global.url => cloud.default.url\n', err.getvalue()) self.assertEqual(sample, _cnf.get_cloud('default', 'url')) del _cnf content3 = list(content0) content3.insert( 2, 'url = http://example1.com\nurl = http://example2.com\n') with make_file(content3) as f: _cnf = Config(path=f.name) self.assertEqual([], _cnf.rescue_old_file(err=err)) self.assertEqual( 2 * '... rescue global.url => cloud.default.url\n', err.getvalue()) self.assertEqual( 'http://example2.com', _cnf.get_cloud('default', 'url')) del _cnf content4 = list(content0) content4.insert(2, 'url = http://example1.com\n') content4.append('\n[cloud "default"]\nurl=http://example2.com\n') with make_file(content4) as f: _cnf = Config(path=f.name) from kamaki.cli.errors import CLISyntaxError self.assertRaises(CLISyntaxError, _cnf.rescue_old_file) del _cnf content5 = list(content0) extras = [ ('pithos_cli', 'pithos'), ('store_cli', 'pithos'), ('storage_cli', 'pithos'), ('compute_cli', 'cyclades'), ('cyclades_cli', 'cyclades')] for sample in extras: content5.insert(2, '%s = %s\n' % sample) with make_file(content5) as f: _cnf = Config(path=f.name) self.assertEqual( sorted(['global.%s = %s' % sample for sample in extras]), sorted(_cnf.rescue_old_file()))
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE. # # The views and conclusions contained in the software and # documentation are those of the authors and should not be # interpreted as representing official policies, either expressed # or implied, of GRNET S.A. from kamaki.cli.config import Config from kamaki.clients import astakos, image, ClientError from kamaki.clients.utils import https https.patch_with_certs("/etc/ssl/certs/ca-certificates.crt") cnf = Config() CLOUD = cnf.get("global", "default_cloud") URL = cnf.get_cloud(CLOUD, "url") TOKEN = cnf.get_cloud(CLOUD, "token") identity_client = astakos.CachedAstakosClient(URL, TOKEN) imageURL = identity_client.get_endpoint_url(image.ImageClient.service_type) image_client = image.ImageClient(imageURL, TOKEN) location = (identity_client.user_info()["id"], "images", "my_image.diskdump") properties = dict(osfamily="linux", users="root", os="debian") img = image_client.register("My New Image", location, properties=properties) image_client.unregister(img["id"])