summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLuke Kanies <luke@madstop.com>2005-05-11 19:05:39 +0000
committerLuke Kanies <luke@madstop.com>2005-05-11 19:05:39 +0000
commitec88acf10421b4c2dba38f154d5adcf9ea9202d2 (patch)
tree463159539b0c9d08142c47a859b8334bceeff6f3
parent42dadad734a71dd10b059747b927394b4ef31ea6 (diff)
downloadpuppet-ec88acf10421b4c2dba38f154d5adcf9ea9202d2.tar.gz
puppet-ec88acf10421b4c2dba38f154d5adcf9ea9202d2.tar.xz
puppet-ec88acf10421b4c2dba38f154d5adcf9ea9202d2.zip
i can basically actually do work now
git-svn-id: https://reductivelabs.com/svn/puppet/library/trunk@240 980ebf18-57e1-0310-9a29-db15c13687c0
-rw-r--r--lib/blink/client.rb10
-rw-r--r--lib/blink/statechange.rb10
-rw-r--r--lib/blink/transaction.rb14
-rw-r--r--lib/blink/type.rb37
-rw-r--r--lib/blink/type/file.rb126
-rw-r--r--lib/blink/type/service.rb30
-rw-r--r--lib/blink/type/state.rb12
-rw-r--r--test/blinktest.rb3
8 files changed, 166 insertions, 76 deletions
diff --git a/lib/blink/client.rb b/lib/blink/client.rb
index 361301181..424aa379b 100644
--- a/lib/blink/client.rb
+++ b/lib/blink/client.rb
@@ -30,33 +30,27 @@ module Blink
def objects=(list)
objects = []
list.collect { |object|
- Blink.verbose "object %s" % [object]
# create a Blink object from the list...
#puts "yayness"
if type = Blink::Type.type(object.type)
namevar = type.namevar
- puts object.inspect
if namevar != :name
object[namevar] = object[:name]
object.delete(:name)
end
- puts object.inspect
begin
- puts object.inspect
typeobj = type.new(object)
- Blink.verbose "object %s is %s" % [object,typeobj]
objects.push typeobj
rescue => detail
puts "Failed to create object: %s" % detail
- puts object.class
- puts object.inspect
+ #puts object.class
+ #puts object.inspect
exit
end
else
raise "Could not find object type %s" % object.type
end
}
- Blink.verbose "object length is %s" % objects.length
# okay, we have a list of all of the objects we're supposed
# to execute
diff --git a/lib/blink/statechange.rb b/lib/blink/statechange.rb
index 17aef41fe..e0b0a6794 100644
--- a/lib/blink/statechange.rb
+++ b/lib/blink/statechange.rb
@@ -21,13 +21,19 @@ module Blink
#---------------------------------------------------------------
def forward
- @type.change(@path,@is,@should)
+ Blink.notice "moving change forward"
+ if @state.noop
+ Blink.notice "change noop is %s" % @state.noop
+ else
+ @state.sync
+ end
end
#---------------------------------------------------------------
#---------------------------------------------------------------
def backward
- @type.change(@path,@should,@is)
+ raise "Moving statechanges backward is currently unsupported"
+ #@type.change(@path,@should,@is)
end
#---------------------------------------------------------------
diff --git a/lib/blink/transaction.rb b/lib/blink/transaction.rb
index 9ff4ea95d..7c0a86407 100644
--- a/lib/blink/transaction.rb
+++ b/lib/blink/transaction.rb
@@ -21,24 +21,21 @@ class Blink::Transaction
# for now, just store the changes for executing linearly
# later, we might execute them as we receive them
def change(change)
- Blink.notice "adding change"
@changes.push change
end
#---------------------------------------------------------------
#---------------------------------------------------------------
def evaluate
+ Blink.notice "evaluating %s changes" % @changes.length
@changes.each { |change|
- next if change.noop
-
- msg = change.sync
+ msg = change.forward
}
end
#---------------------------------------------------------------
#---------------------------------------------------------------
def initialize(tree)
- Blink.notice "tree is %s" % [tree]
@tree = tree
@collect = true
@changes = []
@@ -47,17 +44,16 @@ class Blink::Transaction
#---------------------------------------------------------------
def run
+ Blink.notice "running transaction"
if @tree.is_a?(Array)
- Blink.notice "running array transaction"
@tree.each { |item|
item.evaluate(self)
}
else
- Blink.notice "running transaction"
@tree.evaluate(self)
end
- Blink.notice "[%s]" % [@changes]
- #self.evaluate
+ Blink.notice "finished transaction"
+ self.evaluate
end
#---------------------------------------------------------------
end
diff --git a/lib/blink/type.rb b/lib/blink/type.rb
index 0541c5e8d..f74c492da 100644
--- a/lib/blink/type.rb
+++ b/lib/blink/type.rb
@@ -183,8 +183,8 @@ class Blink::Type < Blink::Element
if @objects.has_key?(newobj.name)
puts @objects
- raise "'#{newobj.name}' already exists in " +
- "class '#{newobj.class}': #{@objects[newobj.name]}"
+ raise "Object '%s' of type '%s' already exists" %
+ [newobj.name,newobj.class.name]
else
#Blink.debug("adding %s of type %s to class list" %
# [object.name,object.class])
@@ -243,6 +243,12 @@ class Blink::Type < Blink::Element
#---------------------------------------------------------------
#---------------------------------------------------------------
+ def Type.states
+ return @states
+ end
+ #---------------------------------------------------------------
+
+ #---------------------------------------------------------------
def Type.validstate(name)
unless @validstates.length == @states.length
self.buildstatehash
@@ -364,6 +370,12 @@ class Blink::Type < Blink::Element
hash.delete(:check)
end
+ if hash.include?("noop")
+ Blink.notice "deleting noop (%s)" % hash["noop"]
+ @noop = hash["noop"]
+ hash.delete("noop")
+ end
+
# states and parameters are treated equivalently from the outside:
# as name-value pairs (using [] and []=)
# internally, however, parameters are merely a hash, while states
@@ -437,9 +449,19 @@ class Blink::Type < Blink::Element
# we ignore parameters here, because they only modify how work gets
# done, they don't ever actually result in work specifically
def each
- # we're only interested in the actual states, not the name/state
- # pairs
- [@children,@states.values].flatten.each { |child|
+ # we want to return the states in the order that each type
+ # specifies it, because it may (as in the case of File#create)
+ # be important
+ tmpstates = []
+ self.class.states.each { |state|
+ if @states.include?(state.name)
+ tmpstates.push(@states[state.name])
+ end
+ }
+ unless tmpstates.length == @states.length
+ raise "Something went very wrong with tmpstates creation"
+ end
+ [@children,tmpstates].flatten.each { |child|
yield child
}
end
@@ -463,11 +485,6 @@ class Blink::Type < Blink::Element
# states
def evaluate(transaction)
self.each { |child|
- if child.is_a?(Blink::State)
- Blink.verbose "Got state"
- else
- Blink.verbose "type is %s" % self.class
- end
child.evaluate(transaction)
}
end
diff --git a/lib/blink/type/file.rb b/lib/blink/type/file.rb
index 82b4f574c..20587a6b1 100644
--- a/lib/blink/type/file.rb
+++ b/lib/blink/type/file.rb
@@ -11,6 +11,30 @@ module Blink
# because the objects must be defined for us to use them in our
# definition of the file object
class State
+ class FileCreate < Blink::State
+ require 'etc'
+ attr_accessor :file
+ @name = :create
+
+ def retrieve
+ stat = nil
+
+ self.is = FileTest.exist?(self.parent[:path])
+ Blink.debug "exists state is %s" % self.is
+ end
+
+
+ def sync
+ begin
+ File.open(self.path,"w") { # just create an empty file
+ }
+ rescue => detail
+ raise detail
+ end
+ #self.parent.newevent(:event => :inode_changed)
+ end
+ end
+
class FileUID < Blink::State
require 'etc'
attr_accessor :file
@@ -22,8 +46,9 @@ module Blink
begin
stat = File.stat(self.parent[:path])
rescue
- # this isn't correct, but what the hell
- raise "File '%s' does not exist: #{$!}" % self.parent[:path]
+ self.is = -1
+ Blink.debug "chown state is %d" % self.is
+ return
end
self.is = stat.uid
@@ -31,32 +56,28 @@ module Blink
begin
user = Etc.getpwnam(self.should)
if user.gid == ""
- raise "Could not retrieve uid for %s" % self.parent
+ raise "Could not retrieve uid for '%s'" % self.parent
end
- Blink.debug "converting %s to integer %d" %
+ Blink.debug "converting %s to integer '%d'" %
[self.should,user.uid]
self.should = user.uid
rescue
- raise "Could not get any info on user %s" % self.should
+ raise "Could not get any info on user '%s'" % self.should
end
end
Blink.debug "chown state is %d" % self.is
end
- #def <=>(other)
- # if other.is_a?(Integer)
- # begin
- # other = Etc.getpwnam(other).uid
- # rescue
- # raise "Could not get uid for #{@params[:uid]}"
- # end
- # end
-#
-# self.is <=> other
-# end
-
def sync
begin
+ stat = File.stat(self.parent[:path])
+ rescue => error
+ Blink.error "File '%s' does not exist; cannot chown" %
+ self.parent[:path]
+ return
+ end
+
+ begin
File.chown(self.should,-1,self.parent[:path])
rescue
raise "failed to sync #{self.parent[:path]}: #{$!}"
@@ -74,21 +95,41 @@ module Blink
@name = :mode
+ def initialize(should)
+ # this is pretty hackish, but i need to make sure the number is in
+ # octal, yet the number can only be specified as a string right now
+ unless should =~ /^0/
+ should = "0" + should
+ end
+ should = Integer(should)
+ super(should)
+ end
+
def retrieve
stat = nil
begin
stat = File.stat(self.parent[:path])
+ self.is = stat.mode & 007777
rescue => error
- raise "File %s could not be stat'ed: %s" % [self.parent[:path],error]
+ # a value we know we'll never get in reality
+ self.is = -1
+ return
end
- self.is = stat.mode & 007777
Blink.debug "chmod state is %o" % self.is
end
def sync
begin
+ stat = File.stat(self.parent[:path])
+ rescue => error
+ Blink.error "File '%s' does not exist; cannot chmod" %
+ self.parent[:path]
+ return
+ end
+
+ begin
File.chmod(self.should,self.parent[:path])
rescue
raise "failed to chmod #{self.parent[:path]}: #{$!}"
@@ -131,8 +172,9 @@ module Blink
begin
stat = File.stat(self.parent[:path])
rescue
- # this isn't correct, but what the hell
- raise "File #{self.parent[:path]} does not exist: #{$!}"
+ self.is = -1
+ Blink.debug "chgrp state is %d" % self.is
+ return
end
self.is = stat.gid
@@ -159,30 +201,17 @@ module Blink
Blink.debug "chgrp state is %d" % self.is
end
-# def <=>(other)
-# # unless we're numeric...
-# if other.is_a?(Integer)
-# begin
-# group = Etc.getgrnam(other)
-# # yeah, don't ask me
-# # this is retarded
-# #p group
-# other = group.gid
-# if other == ""
-# raise "Could not retrieve gid for %s" % other
-# end
-# rescue
-# raise "Could not get any info on group %s" % other
-# end
-# end
-#
-# #puts self.should
-# self.is <=> other
-# end
-
def sync
Blink.debug "setting chgrp state to %d" % self.should
begin
+ stat = File.stat(self.parent[:path])
+ rescue => error
+ Blink.error "File '%s' does not exist; cannot chgrp" %
+ self.parent[:path]
+ return
+ end
+
+ begin
# set owner to nil so it's ignored
File.chown(nil,self.should,self.parent[:path])
rescue
@@ -198,6 +227,7 @@ module Blink
attr_reader :stat, :path, :params
# class instance variable
@states = [
+ Blink::State::FileCreate,
Blink::State::FileUID,
Blink::State::FileGroup,
Blink::State::FileMode,
@@ -210,6 +240,18 @@ module Blink
@name = :file
@namevar = :path
+
+ def sync
+ if self.create and ! FileTest.exist?(self.path)
+ begin
+ File.open(self.path,"w") { # just create an empty file
+ }
+ rescue => detail
+ raise detail
+ end
+ end
+ super
+ end
end # Blink::Type::File
end # Blink::Type
diff --git a/lib/blink/type/service.rb b/lib/blink/type/service.rb
index d65464b25..83f780c4e 100644
--- a/lib/blink/type/service.rb
+++ b/lib/blink/type/service.rb
@@ -13,6 +13,25 @@ module Blink
class ServiceRunning < State
@name = :running
+ # this whole thing is annoying
+ # i should probably just be using booleans, but for now, i'm not...
+ def initialize(should)
+ if should == false
+ should = 0
+ elsif should == true
+ should = 1
+ elsif should == "1" or should == 1
+ should = 1
+ elsif should == "0" or should == 0
+ should = 0
+ else
+ Blink.warning "%s: interpreting '%s' as false" %
+ [self.class,should]
+ should = 0
+ end
+ super(should)
+ end
+
def retrieve
self.is = self.running()
Blink.debug "Running value for '%s' is '%s'" %
@@ -49,7 +68,7 @@ module Blink
if status < 1
Blink.debug "Starting '%s'" % self
unless self.parent.initcmd("start")
- raise "Failed to start %s" % self.name
+ raise "Failed to start '%s'" % self.parent.name
end
else
Blink.debug "'%s' is already running, yo" % self
@@ -107,6 +126,7 @@ module Blink
# if we've gotten this far, we found a valid script
return fqname
}
+ raise "Could not find init script for '%s'" % name
end
# it'd be nice if i didn't throw the output away...
@@ -115,14 +135,14 @@ module Blink
def initcmd(cmd)
script = self.initscript
- #Blink.debug "Executing '%s %s' as initcmd for '%s'" %
- # [script,cmd,self]
+ Blink.debug "Executing '%s %s' as initcmd for '%s'" %
+ [script,cmd,self]
rvalue = Kernel.system("%s %s" %
[script,cmd])
- #Blink.debug "'%s' ran with exit status '%s'" %
- # [cmd,rvalue]
+ Blink.debug "'%s' ran with exit status '%s'" %
+ [cmd,rvalue]
rvalue
diff --git a/lib/blink/type/state.rb b/lib/blink/type/state.rb
index 04b45b246..693bd35f9 100644
--- a/lib/blink/type/state.rb
+++ b/lib/blink/type/state.rb
@@ -70,6 +70,18 @@ class Blink::State < Blink::Element
#---------------------------------------------------------------
#---------------------------------------------------------------
+ # for testing whether we should actually do anything
+ def noop
+ unless defined? @noop
+ @noop = false
+ end
+ tmp = @noop || self.parent.noop || Blink[:noop] || false
+ Blink.notice "noop is %s" % tmp
+ return tmp
+ end
+ #---------------------------------------------------------------
+
+ #---------------------------------------------------------------
def refresh(transaction)
self.retrieve
diff --git a/test/blinktest.rb b/test/blinktest.rb
index 99e0e207a..73a6d560e 100644
--- a/test/blinktest.rb
+++ b/test/blinktest.rb
@@ -38,6 +38,9 @@ unless defined? BlinkTestSuite
def textfiles
textdir = File.join($blinkbase,"examples","code")
+ # only parse this one file now
+ yield File.join(textdir,"head")
+ return
files = Dir.entries(textdir).reject { |file|
file =~ %r{\.swp}
}.reject { |file|