Permalink
Browse files

initial commit

  • Loading branch information...
0 parents commit e1ddee218e7426f5f7a22eb690fef078a3a6b661 Dan Bode committed Nov 14, 2012
@@ -0,0 +1,2 @@
+.idea
+*swp
47 GOAL
@@ -0,0 +1,47 @@
+
+cloudstack_server { 'host_name':
+ host_name => 'host_name',
+ service_name => 'service_name',
+ zone_name => 'zone_name',
+ network_names => ['network_name'],
+ account_name => 'account_name',
+ account_domain => 'account_domain',
+ group => 'group',
+}
+
+
+
+define local_cloudstack_server (
+ $host_name = $name,
+ $service_name,
+ $template_name,
+ $zone_name = undef,
+ $network_names = [],
+ $account_name = undef,
+ $account_domain = undef,
+ $work_label = undef,
+ $dtap_stage = undef
+) {
+
+ $group = structure2json( {
+ work_label => $work_label,
+ $dtap_stage => dtap_stage
+ } )
+
+ cloudstack_server { $host_name:
+ service_name => $service_name,
+ zone_name => $zone_name,
+ network_names => $network_names,
+ account_name => $account_name,
+ account_domain => $account_domain,
+ group => $group,
+ }
+
+}
+
+
+more goals
+
+- list default settings?
+- associate and disassociate ips
+- port forwarding rules?
@@ -0,0 +1,42 @@
+== About
+
+== Setting up auth credentials.
+
+your credentials should be stored in a file called transport.yaml in your puppet conf dir.
+
+(for users this is ~/.puppet/, for root, this is /etc/puppet, basically where
+puppet.conf lives)
+
+Project that create Puppet resource's for modeling CloudStack objects.
+cloudstack:
+ api_key: 'api_key'
+ secret_access_key: ecret_key'
+ host: '127.0.0.1'
+ port: '8080'
+ path: '/client/api'
+ scheme: 'http'
+
+== querying for resources, now you can use puppet resource to do all kinds of stuff
+
+* list zones
+
+puppet resource cloudstack_zone
+
+* list flavors
+
+puppet resource cloudstack_flavor
+
+* list images
+
+puppet resource cloudstack_image
+
+* list networks
+
+puppet resource cloudstack_network
+
+* list instances
+
+puppet resource cloudstack_instnace
+
+check the file tests/example.pp, it shows how to create instances as resources,
+only resources support being managed at the moment.
@@ -0,0 +1,74 @@
+require File.join(
+ File.dirname(__FILE__),
+ '..',
+ '..',
+ 'puppet_x',
+ 'cloudstack',
+ 'transport.rb'
+ )
+
+class Puppet::Provider::CloudStack < Puppet::Provider
+ # To change this template use File | Settings | File Templates.
+
+ def self.prefetch(resources)
+ if self.respond_to?(:instances)
+ vms = instances
+ resources.keys.each do |name|
+ if provider = vms.find{ |pkg| pkg.name == name }
+ resources[name].provider = provider
+ end
+ end
+ end
+ end
+
+ def self.connection(catalog=nil)
+ #require 'ruby-debug';debugger
+ # there is no provider at the moment. If there were, it should be fog.
+ @transport ||= Puppet_x::CloudStack::Transport.retrieve(catalog)
+ end
+
+ def connection
+ self.class.connection
+ end
+
+ def exists?
+ @property_hash[:ensure] == :present
+ end
+
+ def create
+ fail_read_only
+ end
+
+ def destroy
+ fail_read_only
+ end
+
+ def fail_read_only
+ raise(Puppet::Error, 'This resource is read only. It does not support being managed')
+ end
+
+ # get id from things that have models in fog
+ def get_id_from_model(name, type, options={})
+ matching = connection.send(type, options).select {|x| x.name == name }
+ if matching.size > 1
+ raise(Puppet::Error, "Found multiple #{type} matching: #{name}, Puppet expects this to be unique. Failing.")
+ elsif matching.empty?
+ raise(Puppet::Error, "Did not find a #{type} mathing: ""#{name}"". Failing.")
+ else
+ matching.first.id
+ end
+ end
+
+ def get_id_from_request(name, type, options={})
+ all_objects = connection.send("list_#{type}s", options)["list#{type}sresponse"][type]
+ matching = all_objects.select {|x| x['name'] == name }
+ if matching.size > 1
+ raise(Puppet::Error, "Found multiple #{type} matching:\"#{name}\", Puppet expects this to be unique. Failing.")
+ elsif matching.empty?
+ raise(Puppet::Error, "Did not find a #{type} mathing: \"#{name}\". Failing.")
+ else
+ matching.first['id']
+ end
+ end
+
+end
@@ -0,0 +1,35 @@
+require File.join(File.dirname(__FILE__), '..', 'cloudstack.rb')
+Puppet::Type.type(:cloudstack_flavor).provide(
+ :default,
+ :parent => Puppet::Provider::CloudStack
+) do
+
+ # self.prefetch do
+ # end
+
+ mk_resource_methods
+
+ def self.instances
+
+ connection.flavors.collect do |flavor|
+ new(
+ :name => flavor.name,
+ :id => flavor.id,
+ :cpu_number => flavor.cpu_number,
+ :memory => flavor.memory,
+ :ensure => :present
+ )
+ end
+ end
+
+ def id=(id)
+ fail_read_only
+ end
+ def cpu_number=(num)
+ fail_read_only
+ end
+ def memory=(mem)
+ fail_read_only
+ end
+
+end
@@ -0,0 +1,28 @@
+require File.join(File.dirname(__FILE__), '..', 'cloudstack.rb')
+Puppet::Type.type(:cloudstack_image).provide(
+ :default,
+ :parent => Puppet::Provider::CloudStack
+) do
+
+ # self.prefetch
+ # end
+
+ mk_resource_methods
+
+ def self.instances
+ conn = connection
+ # I could not use the
+ conn.list_templates('templatefilter' => 'executable')['listtemplatesresponse']['template'].collect do |image|
+ new(
+ :name => image['name'],
+ :id => image['id'],
+ :ensure => :present
+ )
+ end
+ end
+
+ def id=(id)
+ fail_read_only
+ end
+
+end
@@ -0,0 +1,101 @@
+require File.join(File.dirname(__FILE__), '..', 'cloudstack.rb')
+Puppet::Type.type(:cloudstack_instance).provide(
+ 'default',
+ :parent => Puppet::Provider::CloudStack
+) do
+
+ mk_resource_methods
+
+ def self.instances
+ # I may need to fail if the server does not have a name?
+ connection.servers.collect do |server|
+ if server.state != 'Destroyed'
+ if server.nics.size > 1
+ raise(Puppet::Error, "Does not support dual nics (it is just a prototype")
+ end
+ new(
+ :name => server.display_name,
+ :id => server.id,
+ :flavor => server.flavor_name,
+ :image => server.image_name,
+ :zone => server.zone_name,
+ :network_id => server.nics.first['networkid'],
+ :internal_ipaddress => server.nics.first['ipaddress'],
+ :account => server.account_name,
+ :domain => server.domain_name,
+ :host => server.host_name,
+ :state => server.state.downcase,
+ #:keypair => server.keypair,
+ :ensure => :present
+ # I may want to print network information here
+ )
+ end
+ end.compact
+ end
+
+ def create
+ unless resource[:flavor] and resource[:zone] and resource[:image]
+ raise(Puppet::Error, "Must specify flavor, zone, and image names to create an instance")
+ end
+ flavor_id = get_flavor_id(resource[:flavor])
+ zone_id = get_zone_id(resource[:zone])
+ image_id = get_image_id(resource[:image])
+ network_id = resource[:network] ? get_network_id(resource[:network]): nil
+ Puppet.debug("Bootstrapping instance with:
+ :display_name => #{resource[:name]},
+ :image_id => #{image_id},
+ :flavor_id => #{flavor_id},
+ :zone_id => #{zone_id},
+ :network_ids => #{network_id}
+ ")
+ connection.servers.bootstrap(
+ :display_name => resource[:name],
+ :image_id => image_id,
+ :flavor_id => flavor_id,
+ :zone_id => zone_id,
+ :network_ids => network_id
+ #:keypair => resource[:keypair]
+ )
+ end
+
+ def destroy
+ # TODO need to block until delete is completed
+ connection.servers.destroy(@property_hash[:id])
+ end
+
+ # perform adhoc state changes
+ def state=(state)
+ if state == 'running'
+ connection.start_virtual_machine(:id => @property_hash[:id])
+ elsif state == 'stopped'
+ connection.stop_virtual_machine(:id => @property_hash[:id])
+ elsif state == 'reboot'
+ connection.reboot_virtual_machine(:id => @property_hash[:id])
+ end
+ @property_hash[:state] = state
+ end
+
+ def network
+ if nets = connection.list_networks('id' => @property_hash[:network_id])['listnetworksresponse']['network']
+ nets.first['name']
+ end
+ end
+
+ def get_flavor_id(name)
+ get_id_from_model(name, 'flavors')
+ end
+
+ # this uses the request object and not the connection object...
+ def get_image_id(name)
+ get_id_from_request(name, 'template', 'templatefilter' => 'executable')
+ end
+
+ def get_network_id(name)
+ get_id_from_request(name, 'network')
+ end
+
+ def get_zone_id(name)
+ get_id_from_model(name, 'zones')
+ end
+
+end
@@ -0,0 +1,29 @@
+require File.join(File.dirname(__FILE__), '..', 'cloudstack.rb')
+Puppet::Type.type(:cloudstack_network).provide(
+ :default,
+ :parent => Puppet::Provider::CloudStack
+) do
+
+ #commands :example => :example
+
+ mk_resource_methods
+
+ def self.instances
+ connection.list_networks['listnetworksresponse']['network'].collect do |net|
+ new(
+ :name => net['name'],
+ :id => net['id'],
+ :domain => net['domain'],
+ :cidr => net['cidr'],
+ :ensure => :present
+ )
+ end
+ end
+
+ def create
+ end
+
+ def destroy
+ end
+
+end
Oops, something went wrong.

0 comments on commit e1ddee2

Please sign in to comment.