Skip to content

Instantly share code, notes, and snippets.

@coderanger
Forked from Sauraus/editor.rb
Last active October 14, 2015 20:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coderanger/137eeda65f188c208013 to your computer and use it in GitHub Desktop.
Save coderanger/137eeda65f188c208013 to your computer and use it in GitHub Desktop.
Unity cookbook
require 'chef/resource'
require 'chef/provider'
module Unity
module Resources
module UnityEditor
# A 'unity_editor' resource to install and uninstall Unity Editor.
class Resource < Chef::Resource::LWRPBase
self.resource_name = :unity_editor
provides(:unity_editor)
actions(:install, :uninstall)
attribute(:version, kind_of: String, name_attribute: true)
attribute(:url, kind_of: String, required: true)
attribute(:checksum, kind_of: String, required: true)
attribute(:app, kind_of: String, required: true) if Chef.node.platform_family?('mac_os_x')
attribute(:install_root, kind_of: String, required: true, default: (Chef.node.platform_family?('mac_os_x')) ? '/Applications' : 'C:\Applications')
attribute(:force, kind_of: [TrueClass, FalseClass], default: FalseClass)
end
class Provider < Chef::Provider::LWRPBase
provides(:unity_editor)
use_inline_resources
def action_install
return if exists
case node['plaform_family']
when 'mac_os_x'
editor_install_mac_os_x
when 'windows'
editor_install_windows
end
end
def action_uninstall
editor_uninstall unless exists
end
def editor_install_mac_os_x
# Conditional to test whether Unity version is 5.x.x or greater
# If not, use "dmg" cookbook to handle the .dmg file
# Else use "remote_file" to download PKG file and "execute" to install
if new_resource.url.end_with?('dmg')
dmg_package "Unity3D" do
app new_resource.app_name
source new_resource.url
checksum new_resource.checksum
volumes_dir 'Unity Installer'
type 'pkg'
action :install
end
elsif new_resource.url.end_with?('pkg')
pkg_file = "#{Chef::Config[:file_cache_path]}/#{new_resource.app_name}.pkg"
remote_file pkg_file do
source new_resource.url
checksum new_resource.checksum
end
execute "installer -pkg '#{pkg_file}' -target /" do
user 'root'
end
end
# Remove folder if exists and we force the install.
if Dir.exist? install_dir
::Chef.Log.warn "Current installation dir already exists!! #{install_dir}"
execute "rm -rf #{install_dir}" if new_resource.force?
end
# Default installs /Applications/Unity, so move it in case we want multiple installs
execute "mv -f /Applications/Unity #{install_dir}" do
only_if { Dir.exist?("/Applications/Unity") }
end
end
def editor_install_windows
windows_package 'Unity' do
source new_resource.url
checksum new_resource.checksum
version new_resource.version
options "/S /AllUsers /D=#{install_dir}"
installer_type :custom
action :install
end
end
def editor_uninstall
directory new_resource.install_dir do
recursive true
action :delete
end
end
def install_dir
::File.join(new_resource.install_root, "Unity_#{new_resource.version}")
end
def exists
new_resource.force ? false : Dir.exists?(install_dir)
end
end
end
end
end
platform = node['platform_family']
(%w(windows mac_os_x).include?(platform)) ?
unity_versions = data_bag(node['unity']['editor'][platform]['databag']) :
(raise "Unsupported platform [#{platform}] for Unity Editor install")
unity_versions.each do |version|
unity = data_bag_item(node['unity']['editor'][platform]['databag'], version)
Chef::Log.info "Processing Unity Editor version: #{unity['id']}"
unity_editor unity['id'] do
url unity['url']
checksum unity['checksum']
app unity['app'] if platform.eql?('mac_os_x')
install_root ['unity']['editor'][platform]['install_root']
action unity['action']
end
end
@Sauraus
Copy link

Sauraus commented Oct 14, 2015

@coderanger are you aware of any recent LWRP changes in Chef that would break this code?

I am now stumped as this code stopped working on my Chef server and I cannot explain it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment