saklient さくらのクラウド クライアントライブラリ

さくらのクラウド上のリソースを簡単にコントロールするためのライブラリです。PHP, Perl, Ruby, Python, C# 等の言語でご利用いただけます。
An easy interface to control your resources on SAKURA Cloud.

DEPRECATED   本ライブラリは廃止されました。代替ライブラリをご利用ください。

サーバの作成 Create a server

saklient.node(Node.js版)の実装は実験的なものです。
Node.jsでは node-sacloud をご利用いただけます。

PHP (Beta)

<?php


require_once 'vendor/autoload.php';
use Saklient\Cloud\API;




# settings
$token = $argv[1];
$secret = $argv[2];
$zone = $argv[3];
$name = 'saklient demo';
$description = 'saklient demo';
$tag = 'saklient-test';
$cpu = 1;
$mem = 2;
$hostName = 'saklient-test';
$password = 'C8#mf92mp!*s';
$sshPublicKey = 'ssh-rsa AAAA...';

# authorize
$api = API::authorize
                ($token,$secret,$zone);

# search archives
echo "searching archives\n";
$archives = $api->archive
  ->withNameLike('CentOS 6.5 64bit')
  ->withSizeGib(20)
  ->withSharedScope()
  ->limit(1)
  ->find();
$archive = $archives[0];

# search scripts
echo "searching scripts\n";
$scripts = $api->script
  ->withNameLike('WordPress')
  ->withSharedScope()
  ->limit(1)
  ->find();
$script = $scripts[0];

# create a disk
echo "creating a disk\n";
$disk = $api->disk->create();
$disk->name = $name;
$disk->description = $description;
$disk->tags = [$tag];
$disk->plan = $api->product->disk->ssd;
$disk->source = $archive;
$disk->save();

# create a server
echo "creating a server\n";
$server = $api->server->create();
$server->name = $name;
$server->description = $description;
$server->tags = [$tag];
$server->plan = $api->product->server
              ->getBySpec($cpu, $mem);
$server->save();

# connect to shared segment
echo "connecting the server" .
               " to shared segment\n";
$iface = $server->addIface();
$iface->connectToSharedSegment();

# wait disk copy
echo "waiting disk copy\n";
if (!$disk->sleepWhileCopying()) {
  die('failed');
}

# connect the disk to the server
echo "connecting the disk\n";
$disk->connectTo($server);

# config the disk
$diskconf = $disk->createConfig();
$diskconf->hostName = $hostName;
$diskconf->password = $password;
$diskconf->sshKey = $sshPublicKey;
$diskconf->scripts[] = $script;
$diskconf->write();

# boot
echo "booting the server\n";
$server->boot();

# stop
sleep(3);
echo "stopping the server\n";
$server->stop();
if (!$server->sleepUntilDown()) {
  die('failed');
}

# disconnect the disk from the server
echo "disconnecting the disk\n";
$disk->disconnect();

# delete the server
echo "deleting the server\n";
$server->destroy();

# delete the disk
echo "deleting the disk\n";
$disk->destroy();



C# (Beta)

using System;
using System.Collections.Generic;
using System.Threading;
using Saklient.Cloud;
using Saklient.Cloud.Resources;

class CreateServerExample {
 static void Main(string[] args) {
  
  // settings
  string token = args[0];
  string secret = args[1];
  string zone = args[2];
  string name = "saklient demo";
  string description = "saklient demo";
  string tag = "saklient-test";
  int cpu = 1;
  int mem = 2;
  string hostName = "saklient-test";
  string password = "C8#mf92mp!*s";
  string sshPublicKey = "ssh-rsa AAAA...";
  
  // authorize
  API api = API.Authorize(token,secret,zone);
  
  
  // search archives
  Console.WriteLine("searching archives");
  List<Archive> archives = api.Archive
    .WithNameLike("CentOS 6.5 64bit")
    .WithSizeGib(20)
    .WithSharedScope()
    .Limit(1)
    .Find();
  Archive archive = archives[0];
  
  // search scripts
  Console.WriteLine("searching scripts");
  List<Script> scripts = api.Script
    .WithNameLike("WordPress")
    .WithSharedScope()
    .Limit(1)
    .Find();
  Script script = scripts[0];
  
  // create a disk
  Console.WriteLine("creating a disk");
  Disk disk = api.Disk.Create();
  disk.Name = name;
  disk.Description = description;
  disk.Tags = new List<string> {tag};
  disk.Plan = api.Product.Disk.Ssd;
  disk.Source = archive;
  disk.Save();
  
  // create a server
  Console.WriteLine("creating a server");
  Server server = api.Server.Create();
  server.Name = name;
  server.Description = description;
  server.Tags = new List<string> {tag};
  server.Plan = api.Product.Server
                        .GetBySpec(cpu, mem);
  server.Save();
  
  // connect to shared segment
  Console.WriteLine("connecting the server"+
                       " to shared segment");
  Iface iface = server.AddIface();
  iface.ConnectToSharedSegment();
  
  // wait disk copy
  Console.WriteLine("waiting disk copy");
  if (!disk.SleepWhileCopying()) {
    throw new Exception("failed");
  }
  
  // connect the disk to the server
  Console.WriteLine("connecting the disk");
  disk.ConnectTo(server);
  
  // config the disk
  DiskConfig diskconf = disk.CreateConfig();
  diskconf.HostName = hostName;
  diskconf.Password = password;
  diskconf.SshKey = sshPublicKey;
  diskconf.Scripts.Add(script);
  diskconf.Write();
  
  // boot
  Console.WriteLine("booting the server");
  server.Boot();
  
  // stop
  Thread.Sleep(3);
  Console.WriteLine("stopping the server");
  server.Stop();
  if (!server.SleepUntilDown()) {
    throw new Exception("failed");
  }
  
  // disconnect the disk from the server
  Console.WriteLine("disconnecting the disk");
  disk.Disconnect();
  
  // delete the server
  Console.WriteLine("deleting the server");
  server.Destroy();
  
  // delete the disk
  Console.WriteLine("deleting the disk");
  disk.Destroy();
  
 }
}

Python (Beta)

#!/usr/local/bin/python3

import sys, time

from saklient.cloud.api import API




# settings
token = sys.argv[1]
secret = sys.argv[2]
zone = sys.argv[3]
name = 'saklient demo'
description = 'saklient demo'
tag = 'saklient-test'
cpu = 1
mem = 2
host_name = 'saklient-test'
password = 'C8#mf92mp!*s'
ssh_public_key = 'ssh-rsa AAAA...'

# authorize
api = API.authorize(token,secret,zone)


# search archives
print('searching archives')
archives = api.archive \
  .with_name_like('CentOS 6.5 64bit') \
  .with_size_gib(20) \
  .with_shared_scope() \
  .limit(1) \
  .find()
archive = archives[0]

# search scripts
print('searching scripts')
scripts = api.script \
  .with_name_like('WordPress') \
  .with_shared_scope() \
  .limit(1) \
  .find()
script = scripts[0]

# create a disk
print('creating a disk')
disk = api.disk.create()
disk.name = name
disk.description = description
disk.tags = [tag]
disk.source = archive
disk.plan = api.product.disk.ssd
disk.save()

# create a server
print('creating a server')
server = api.server.create()
server.name = name
server.description = description
server.tags = [tag]
server.plan = api.product.server \
                 .get_by_spec(cpu, mem)
server.save()

# connect to shared segment
print('connecting the server' +\
                  ' to shared segment')
iface = server.add_iface()
iface.connect_to_shared_segment()

# wait disk copy
print('waiting disk copy')
if not disk.sleep_while_copying():
    raise Exception('failed')


# connect the disk to the server
print('connecting the disk')
disk.connect_to(server)

# config the disk
diskconf = disk.create_config()
diskconf.host_name = host_name
diskconf.password = password
diskconf.ssh_key = ssh_public_key
diskconf.scripts.append(script)
diskconf.write()

# boot
print('booting the server')
server.boot()

# stop
time.sleep(3)
print('stopping the server')
server.stop()
if not server.sleep_until_down():
    raise Exception('failed')


# disconnect the disk from the server
print('disconnecting the disk')
disk.disconnect()

# delete the server
print('deleting the server')
server.destroy()

# delete the disk
print('deleting the disk')
disk.destroy()



Ruby (Beta)

#!/usr/bin/ruby



require 'saklient/cloud/api'




# settings
token = $*[0]
secret = $*[1]
zone = $*[2]
name = 'saklient demo'
description = 'saklient demo'
tag = 'saklient-test'
cpu = 1
mem = 2
host_name = 'saklient-test'
password = 'C8#mf92mp!*s'
ssh_public_key = 'ssh-rsa AAAA...'

# authorize
api = Saklient::Cloud::API::
       authorize(token, secret, zone)

# search archives
puts 'searching archives'
archives = api.archive.
  with_name_like('CentOS 6.5 64bit').
  with_size_gib(20).
  with_shared_scope.
  limit(1).
  find
archive = archives[0]

# search scripts
puts 'searching scripts'
scripts = api.script.
  with_name_like('WordPress').
  with_shared_scope.
  limit(1).
  find
script = scripts[0]

# create a disk
puts 'creating a disk'
disk = api.disk.create
disk.name = name
disk.description = description
disk.tags = [tag]
disk.plan = api.product.disk.ssd
disk.source = archive
disk.save

# create a server
puts 'creating a server'
server = api.server.create
server.name = name
server.description = description
server.tags = [tag]
server.plan = api.product.server.
               get_by_spec(cpu, mem)
server.save

# connect to shared segment
puts 'connecting the server' +
                  ' to shared segment'
iface = server.add_iface
iface.connect_to_shared_segment

# wait disk copy
puts 'waiting disk copy'
disk.sleep_while_copying or
                       abort 'failed'


# connect the disk to the server
puts 'connecting the disk'
disk.connect_to(server)

# config the disk
diskconf = disk.create_config
diskconf.host_name = host_name
diskconf.password = password
diskconf.ssh_key = ssh_public_key
diskconf.scripts.push(script)
diskconf.write

# boot
puts 'booting the server'
server.boot

# stop
sleep 3
puts 'stopping the server'
server.stop
server.sleep_until_down or
                       abort 'failed'


# disconnect the disk from the server
puts 'disconnecting the disk'
disk.disconnect

# delete the server
puts 'deleting the server'
server.destroy

# delete the disk
puts 'deleting the disk'
disk.destroy



Perl (Alpha)

#!/usr/bin/perl

use strict;
use warnings;
use Saklient::Cloud::API;




# settings
my $token = $ARGV[0];
my $secret = $ARGV[1];
my $zone = $ARGV[2];
my $name = 'saklient demo';
my $description = 'saklient demo';
my $tag = 'saklient-test';
my $cpu = 1;
my $mem = 2;
my $host_name = 'saklient-test';
my $password = 'C8#mf92mp!*s';
my $ssh_public_key = 'ssh-rsa AAAA...';

# authorize
my $api=Saklient::Cloud::API::authorize
              ($token, $secret, $zone);

# search archives
print "searching archives\n";
my $archives = $api->archive
  ->with_name_like('CentOS 6.5 64bit')
  ->with_size_gib(20)
  ->with_shared_scope
  ->limit(1)
  ->find;
my $archive = $archives->[0];

# search scripts
print "searching scripts\n";
my $scripts = $api->script
  ->with_name_like('WordPress')
  ->with_shared_scope
  ->limit(1)
  ->find;
my $script = $scripts->[0];

# create a disk
print "creating a disk\n";
my $disk = $api->disk->create
  ->name($name)
  ->description($description)
  ->tags([$tag])
  ->source($archive)
  ->plan($api->product->disk->ssd)
  ->save;

# create a server
print "creating a server\n";
my $server = $api->server->create
  ->name($name)
  ->description($description)
  ->tags([$tag])
  ->plan($api->product->server
           ->get_by_spec($cpu, $mem))
  ->save;

# connect to shared segment
print "connecting the server" .
               " to shared segment\n";
my $iface = $server->add_iface;
$iface->connect_to_shared_segment;

# wait disk copy
print "waiting disk copy\n";
$disk->sleep_while_copying
                      or die 'failed';


# connect the disk to the server
print "connecting the disk\n";
$disk->connect_to($server);

# config the disk
$disk->create_config
  ->host_name($host_name)
  ->password($password)
  ->ssh_key($ssh_public_key)
  ->add_script($script)
  ->write;

# boot
print "booting the server\n";
$server->boot;

# stop
sleep 3;
print "stopping the server\n";
$server->stop;
$server->sleep_until_down
                      or die 'failed';


# disconnect the disk from the server
print "disconnecting the disk\n";
$disk->disconnect;

# delete the server
print "deleting the server\n";
$server->destroy;

# delete the disk
print "deleting the disk\n";
$disk->destroy;



Node.js (Experimental)





var saklient = require('saklient');




// settings
var token = process.argv[2];
var secret = process.argv[3];
var zone = process.argv[4];
var name = 'saklient demo';
var description = 'saklient demo';
var tag = 'saklient-test';
var cpu = 1;
var mem = 2;
var hostName = 'saklient-test';
var password = 'C8#mf92mp!*s';
var sshPublicKey = 'ssh-rsa AAAA...';

// authorize
var api = saklient.cloud.API.
       authorize(token, secret, zone);

// search archives
console.log('searching archives');
var archives = api.archive.
  withNameLike('CentOS 6.5 64bit').
  withSizeGib(20).
  withSharedScope().
  limit(1).
  find();
var archive = archives[0];

// search scripts
console.log('searching scripts');
var scripts = api.script.
  withNameLike('WordPress').
  withSharedScope().
  limit(1).
  find();
var script = scripts[0];

// create a disk
console.log('creating a disk');
var disk = api.disk.create();
disk.name = name;
disk.description = description;
disk.tags = [tag];
disk.plan = api.product.disk.ssd;
disk.source = archive;
disk.save();

// create a server
console.log('creating a server');
var server = api.server.create();
server.name = name;
server.description = description;
server.tags = [tag];
server.plan = api.product.server.
               getBySpec(cpu, mem);
server.save();

// connect to shared segment
console.log('connecting the server' +
                ' to shared segment');
var iface = server.addIface();
iface.connectToSharedSegment();

// wait disk copy
console.log('waiting disk copy');
if (!disk.sleepWhileCopying()) {
  process.exit(1);
}

// connect the disk to the server
console.log('connecting the disk');
disk.connectTo(server);

// config the disk
var diskconf = disk.createConfig();
diskconf.hostName = hostName;
diskconf.password = password;
diskconf.sshKey = sshPublicKey;
diskconf.scripts.push(script);
diskconf.write();

// boot
console.log('booting the server');
server.boot();

// stop the server
api.sleep(3);
console.log('stopping the server');
server.stop();
if (!server.sleepUntilDown()) {
  process.exit(1);
}

// disconnect the disk from the server
console.log('disconnecting the disk');
disk.disconnect();

// delete the server
console.log('deleting the server');
server.destroy();

// delete the disk
console.log('deleting the disk');
disk.destroy();




ディスクイメージのダウンロード Download a disk image

saklient.node(Node.js版)の実装は実験的なものです。
Node.jsでは node-sacloud をご利用いただけます。

PHP (Beta)

<?php


require_once 'vendor/autoload.php';
use Saklient\Cloud\API;





# settings
$token = $argv[1];
$secret = $argv[2];
$zone = $argv[3];
$srcName = 'GitLab';

# authorize
$api = API::authorize
               ($token,$secret,$zone);

# search the source disk
$disks = $api->disk
  ->withNameLike($srcName)
  ->limit(1)
  ->find();
$disk = $disks[0];

# copy the disk to a new archive
echo "copying the disk ".
                 "to a new archive\n";
$archive = $api->archive->create();
$archive->name = 'Copy:'.$disk->name;
$archive->source = $disk;
$archive->save();
if (!$archive->sleepWhileCopying()) {
  die('failed');
}

# get FTP information
$ftp = $archive->openFtp()->ftpInfo;
echo "FTP information:\n";
echo "  user: ", $ftp->user, "\n";
echo "  pass: ", $ftp->password, "\n";
echo "  host: ", $ftp->hostName, "\n";

# download the archive via FTPS
echo "downloading the archive\n";
$url = 'ftps://' .
  rawurlencode($ftp->user) .':'.
  rawurlencode($ftp->password) .'@'.
  $ftp->hostName . '/archive.img';
$src = fopen($url, 'r');
$dst = fopen('./archive.img', 'w');
stream_copy_to_stream($src, $dst);
fclose($dst);
fclose($src);







# delete the archive after download
echo "deleting the archive\n";
$archive->closeFtp();
$archive->destroy();



C# (Beta)

using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using Saklient.Cloud;
using Saklient.Cloud.Resources;

class DownloadArchiveExample {
 static void Main(string[] args) {
  
  // settings
  string token = args[0];
  string secret = args[1];
  string zone = args[2];
  string srcName = "GitLab";
  
  // authorize
  API api = API.Authorize(token,secret,zone);
  
  
  // search the source disk
  List<Disk> disks = api.Disk
    .WithNameLike(srcName)
    .Limit(1)
    .Find();
  Disk disk = disks[0];
  
  // copy the disk to a new archive
  Console.WriteLine("copying the disk "+
                     "to a new archive");
  Archive archive = api.Archive.Create();
  archive.Name = "Copy:" + disk.Name;
  archive.Source = disk;
  archive.Save();
  if (!archive.SleepWhileCopying()) {
    throw new Exception("failed");
  }
  
  // get FTP information
  FtpInfo ftp = archive.OpenFtp().FtpInfo;
  Console.WriteLine("FTP information:");
  Console.WriteLine("  user: "+ftp.User);
  Console.WriteLine("  pass: "+ftp.Password);
  Console.WriteLine("  host: "+ftp.HostName);
  
  // download the archive via FTPS
  Uri uri = new Uri("ftp://"+ftp.HostName+
                             "/archive.img");
  var req = (FtpWebRequest)FtpWebRequest.
                                 Create(uri);
  req.EnableSsl = true;
  req.Credentials = new NetworkCredential
                    (ftp.User, ftp.Password);
  req.Method =
          WebRequestMethods.Ftp.DownloadFile;
  var res =(FtpWebResponse)req.GetResponse();
  var src = res.GetResponseStream();
  var dst = new FileStream("archive.img",
          FileMode.Create, FileAccess.Write);
  src.CopyTo(dst);
  dst.Close();
  src.Close();
  
  // delete the archive after download
  Console.WriteLine("deleting the archive");
  archive.CloseFtp();
  archive.Destroy();
  
 }
}

Python (Beta)

#!/usr/local/bin/python3

import sys, time
from ftplib import FTP_TLS
from saklient.cloud.api import API





# settings
token = sys.argv[1]
secret = sys.argv[2]
zone = sys.argv[3]
src_name = 'GitLab'

# authorize
api = API.authorize(token,secret,zone)


# search the source disk
disks = api.disk \
  .with_name_like(src_name) \
  .limit(1) \
  .find()
disk = disks[0]

# copy the disk to a new archive
print('copying the disk ' +\
                    'to a new archive')
archive = api.archive.create()
archive.name = 'Copy:' + disk.name
archive.source = disk
archive.save()
if not archive.sleep_while_copying():
    raise Exception('failed')


# get FTP information
ftp = archive.open_ftp().ftp_info
print('FTP information:')
print('  user: %s' % ftp.user)
print('  pass: %s' % ftp.password)
print('  host: %s' % ftp.host_name)

# download the archive via FTPS
print('downloading the archive')
ftps = FTP_TLS(ftp.host_name, \
                ftp.user, ftp.password)
ftps.prot_p()
file = open('archive.img', 'wb')
ftps.retrbinary('RETR archive.img', \
                            file.write)
file.close()
ftps.close()








# delete the archive after download
print('deleting the archive')
archive.close_ftp()
archive.destroy()



Ruby (Beta)

#!/usr/bin/ruby



require 'saklient/cloud/api'





# settings
token = $*[0]
secret = $*[1]
zone = $*[2]
src_name = 'GitLab'

# authorize
api = Saklient::Cloud::API::
        authorize(token, secret, zone)

# search the source disk
disks = api.disk.
  withNameLike(src_name).
  limit(1).
  find()
disk = disks[0]

# copy the disk to a new archive
puts 'copying the disk ' +
                    'to a new archive'
archive = api.archive.create
archive.name = 'Copy:' + disk.name
archive.source = disk
archive.save
archive.sleep_while_copying or
                        abort 'failed'


# get FTP information
ftp = archive.open_ftp.ftp_info
puts 'FTP information:'
puts '  user: ' + ftp.user
puts '  pass: ' + ftp.password
puts '  host: ' + ftp.host_name

# download the archive via FTPS
puts 'downloading the archive'

# ...














# delete the archive after download
puts 'deleting the archive'
archive.close_ftp
archive.destroy



Perl (Alpha)

#!/usr/bin/perl

use strict;
use warnings;
use Saklient::Cloud::API;
use Net::FTPSSL;




# settings
my $token = $ARGV[0];
my $secret = $ARGV[1];
my $zone = $ARGV[2];
my $src_name = 'GitLab';

# authorize
my $api=Saklient::Cloud::API::authorize
              ($token, $secret, $zone);

# search the source disk
my $disks = $api->disk
  ->with_name_like($src_name)
  ->limit(1)
  ->find;
my $disk = $disks->[0];

# copy the disk to a new archive
print "copying the disk " .
                  "to a new archive\n";
my $archive = $api->archive->create
  ->name('Copy:'.$disk->name)
  ->source($disk)
  ->save;
$archive->sleep_while_copying
                      or die('failed');


# get FTP information
my $ftp = $archive->open_ftp->ftp_info;
print "FTP information:\n";
printf "  user: %s\n", $ftp->user;
printf "  pass: %s\n", $ftp->password;
printf "  host: %s\n", $ftp->host_name;

# download the archive via FTPS
print "downloading the archive\n";
my $ftps = Net::FTPSSL
                 ->new($ftp->host_name)
                      or die 'failure';
$ftps->login($ftp->user,$ftp->password)
            or die $ftps->last_message;
$ftps->get('archive.img','archive.img')
            or die $ftps->last_message;
$ftps->quit;








# delete the archive after download
print "deleting the archive\n";
$archive->close_ftp;
$archive->destroy;



Node.js (Experimental)





var saklient = require('saklient');





// settings
var token = process.argv[2];
var secret = process.argv[3];
var zone = process.argv[4];
var srcName = 'GitLab';

// authorize
var api = saklient.cloud.API.
        authorize(token, secret, zone);

// search the source disk
var disks = api.disk.
  withNameLike(srcName).
  limit(1).
  find();
var disk = disks[0];

// copy the disk to a new archive
console.log('copying the disk ' +
                     'to new archive');
var archive = api.archive.create();
archive.name = 'Copy:' + disk.name;
archive.source = disk;
archive.save();
if (!archive.sleepWhileCopying()) {
  process.exit(1);
}

// get FTP information
var ftp = archive.openFtp().ftpInfo;
console.log('FTP information:');
console.log('  user: ' + ftp.user);
console.log('  pass: ' + ftp.password);
console.log('  host: ' + ftp.hostName);

// download the archive via FTPS
console.log('downloading the archive');

// ...














// delete the archive after download
console.log('deleting the archive');
archive.closeFtp();
archive.destroy();