Class: ForemanPackages::Sypackage

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/foreman_packages/sypackage.rb

Instance Method Summary collapse

Instance Method Details

#create_contentObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'app/models/foreman_packages/sypackage.rb', line 72

def create_content
	title_content = "class packages {"
	ForemanPackages::Sypackage.all.each do |package|

		 user_content =  "\n" + "\s\s" + "package{'" + package.name + "':" + "\n\s\s\s\s" + "ensure => '" + package.ensure + "',"  + "\n\s\s" +"}" + "\n"

		file_content =  "\s\s" +  "file{'"+ package.file_path + "':" + "\n\s\s\s\s" + "ensure => '" + package.file_ensure + "'," + "\n\s\s\s\s" + "source => '" + package.file_source + "mode => '" + package.file_mode + "'," + "\n\s\s\s\s" + "onwer => '" + package.file_onwer + "group => '" + package.file_group + "'," + "\n\s\s\s\s" +  "notify=> Service['" + package.file_notify + "'],"  + "\n\s\s\s\s"   + "require=> Package['" + package.file_require + "']," + "\n\s\s" +"}" + "\n\s\s" 

		service_content =  "service{'"+ package.service_name + "':" + "\n\s\s\s\s" + "ensure => '" + package.service_ensure + "'," + "\n\s\s\s\s" + "enable => '" + package.service_enable + "hasstatus => '" + package.service_hasstatus + "'," + "\n\s\s\s\s" + "hasrestart => '" + package.service_hasrestart + "," + "\n\s\s" +"}" + "\n\s\s" 

		title_content += user_content + file_content + service_content
	end


	rs = title_content + "\n" + "}"
	return rs
end

#create_dirObject

after_destory :destory_dir



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/models/foreman_packages/sypackage.rb', line 13

def create_dir
		#目前用 "/home/stdtnt/app/"代替  "/etc/puppet/environments/production/modules/packages"
		#该处需要注意权限问题,当前用户是否具有该目录的操作权限
		root_dir_name = "/etc/puppet/environments/production/modules/packages/"
		#self.destory_dir #默认删除该目录,然后重新创建
		dir_exist_status = File.exist?(root_dir_name) #判断该目录是否存在
		if !dir_exist_status
			#创建目录结构
	Dir::mkdir(root_dir_name)
	if !File.exist?(root_dir_name + "files")
		Dir::mkdir(root_dir_name + "files")
	end

	if !File.exist?(root_dir_name + "manifests")
		Dir::mkdir(root_dir_name + "manifests")
	end
		end

			if File.exist?(root_dir_name + "/files")
 			if File.exist?(root_dir_name + "/files/welcome.conf")
			#生成files下welcome.conf文件
			files_welcome_File = File.new(root_dir_name + "/files/welcome.conf", "w+")
  			files_welcome_File.close
 			end
			end

	if File.exist?(root_dir_name + "/manifests")
		if File.exist?(root_dir_name + "/manifests/init.pp")
			FileUtils.rm_r (root_dir_name + "/manifests/init.pp")
 			end
				#生成manifests下init.pp文件
		main_init_File = File.new(root_dir_name +"/manifests/init.pp", "w+")
			if main_init_File
			   main_init_File.syswrite(self.create_content)
			else
			   puts "Unable to open file!"
			end
		main_init_File.close
			end

		#self.change_dir

end

#destory_dirObject



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/models/foreman_packages/sypackage.rb', line 58

def destory_dir
		 #目前用 "/home/stdtnt/app/"代替  "/etc/puppet/environments/production/modules/users"
		#该处需要注意权限问题,当前用户是否具有该目录的操作权限
		root_dir_name = "/etc/puppet/environments/production/modules/packages"
		#判断该目录是否存在
dir_exist_status = File.exist?(root_dir_name )
		if dir_exist_status
			#使用fileUtils.rm_r 删除该文件夹及其以下所有内容
			if ForemanPackages::Sypackage.all.size  == 0 
				FileUtils.rm_r root_dir_name
			end
		end
end