167 lines
		
	
	
		
			4.7 KiB
		
	
	
	
		
			Ruby
		
	
	
	
	
	
			
		
		
	
	
			167 lines
		
	
	
		
			4.7 KiB
		
	
	
	
		
			Ruby
		
	
	
	
	
	
#
 | 
						|
# Cookbook:: kosmos-bitcoin
 | 
						|
# Recipe:: source
 | 
						|
#
 | 
						|
# The MIT License (MIT)
 | 
						|
#
 | 
						|
# Copyright:: 2020, Kosmos Developers
 | 
						|
#
 | 
						|
# Permission is hereby granted, free of charge, to any person obtaining a copy
 | 
						|
# of this software and associated documentation files (the "Software"), to deal
 | 
						|
# in the Software without restriction, including without limitation the rights
 | 
						|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 | 
						|
# copies of the Software, and to permit persons to whom the Software is
 | 
						|
# furnished to do so, subject to the following conditions:
 | 
						|
#
 | 
						|
# The above copyright notice and this permission notice shall be included in
 | 
						|
# all copies or substantial portions of the Software.
 | 
						|
#
 | 
						|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 | 
						|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 | 
						|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 | 
						|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 | 
						|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 | 
						|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 | 
						|
# THE SOFTWARE.
 | 
						|
#
 | 
						|
 | 
						|
# TODO move to custom kosmos cookbook before publshing bitcoin cookbook
 | 
						|
systemd_unit "mnt-data-bitcoin.mount" do
 | 
						|
  content({
 | 
						|
    Unit: {
 | 
						|
      Description: 'Bitcoin Core data directory',
 | 
						|
    },
 | 
						|
    Mount: {
 | 
						|
      What: '/var/lib/vmshare-bitcoin',
 | 
						|
      Where: '/mnt/data/bitcoin',
 | 
						|
      Type: '9p',
 | 
						|
      Options: 'trans=virtio,version=9p2000.L'
 | 
						|
    },
 | 
						|
    Install: {
 | 
						|
      WantedBy: 'multi-user.target'
 | 
						|
    }
 | 
						|
  })
 | 
						|
  verify false
 | 
						|
  triggers_reload true
 | 
						|
  action [:create, :enable, :start]
 | 
						|
end
 | 
						|
 | 
						|
 | 
						|
include_recipe 'ark'
 | 
						|
 | 
						|
build_essential
 | 
						|
 | 
						|
%w{ libtool autotools-dev make automake cmake curl g++-multilib libtool
 | 
						|
    binutils-gold bsdmainutils pkg-config python3 patch }.each do |pkg|
 | 
						|
  apt_package pkg
 | 
						|
end
 | 
						|
 | 
						|
ark 'bitcoind' do
 | 
						|
  url "https://bitcoincore.org/bin/bitcoin-core-#{node['bitcoin']['version']}/bitcoin-#{node['bitcoin']['version']}.tar.gz"
 | 
						|
  checksum node['bitcoin']['checksum']
 | 
						|
  action :put
 | 
						|
end
 | 
						|
 | 
						|
execute "Compile bitcoin-core dependencies" do
 | 
						|
  cwd "/usr/local/bitcoind/depends"
 | 
						|
  command "make NO_QT=1"
 | 
						|
  not_if { ::File.directory?("/usr/local/bitcoind/depends/x86_64-pc-linux-gnu") }
 | 
						|
end
 | 
						|
 | 
						|
execute "Configure, compile bitcoin-core" do
 | 
						|
  cwd "/usr/local/bitcoind"
 | 
						|
  # FIXME only executes first array item?
 | 
						|
  command [
 | 
						|
    "./autogen.sh",
 | 
						|
    "./configure --prefix=$PWD/depends/x86_64-pc-linux-gnu",
 | 
						|
    "make"
 | 
						|
  ]
 | 
						|
  not_if { ::File.exist?("/usr/local/bitcoind/src/bitcoind") }
 | 
						|
end
 | 
						|
 | 
						|
link "/usr/local/bin/bitcoind" do
 | 
						|
  to "/usr/local/bitcoind/src/bitcoind"
 | 
						|
end
 | 
						|
 | 
						|
link "/usr/local/bin/bitcoin-cli" do
 | 
						|
  to "/usr/local/bitcoind/src/bitcoin-cli"
 | 
						|
end
 | 
						|
 | 
						|
bitcoin_user      = node['bitcoin']['username']
 | 
						|
bitcoin_group     = node['bitcoin']['usergroup']
 | 
						|
bitcoin_datadir   = node['bitcoin']['datadir']
 | 
						|
bitcoin_walletdir = node['bitcoin']['walletdir']
 | 
						|
bitcoin_conf_path = node['bitcoin']['conf_path']
 | 
						|
credentials       = Chef::EncryptedDataBagItem.load('credentials', 'bitcoin')
 | 
						|
 | 
						|
group bitcoin_group
 | 
						|
 | 
						|
user bitcoin_user do
 | 
						|
  manage_home true
 | 
						|
  uid 1006
 | 
						|
  gid bitcoin_group
 | 
						|
  shell "/bin/bash"
 | 
						|
end
 | 
						|
 | 
						|
[bitcoin_datadir, bitcoin_walletdir].each do |path|
 | 
						|
  directory path do
 | 
						|
    owner bitcoin_user
 | 
						|
    group bitcoin_group
 | 
						|
    mode '0750'
 | 
						|
    recursive true
 | 
						|
    action :create
 | 
						|
  end
 | 
						|
end
 | 
						|
 | 
						|
bitcoin_config = node['bitcoin']['conf'].merge({
 | 
						|
  rpcpassword: credentials["rpcpassword"]
 | 
						|
})
 | 
						|
 | 
						|
template bitcoin_conf_path do
 | 
						|
  owner bitcoin_user
 | 
						|
  group bitcoin_group
 | 
						|
  mode '0640'
 | 
						|
  variables conf: bitcoin_config,
 | 
						|
            mainnet_conf: node['bitcoin']['mainnet_conf'],
 | 
						|
            testnet_conf: node['bitcoin']['testnet_conf'],
 | 
						|
            regtest_conf: node['bitcoin']['regtest_conf']
 | 
						|
  action :create
 | 
						|
  notifies :restart, "systemd_unit[bitcoind.service]", :delayed
 | 
						|
end
 | 
						|
 | 
						|
systemd_unit 'bitcoind.service' do
 | 
						|
  content({
 | 
						|
    Unit: {
 | 
						|
      Description: 'Bitcoin Core daemon',
 | 
						|
      Documentation: ['https://bitcoincore.org'],
 | 
						|
      After: 'network.target'
 | 
						|
    },
 | 
						|
    Service: {
 | 
						|
      User: bitcoin_user,
 | 
						|
      Type: 'simple',
 | 
						|
      ExecStart: "bitcoind -conf=#{bitcoin_conf_path} -datadir=#{bitcoin_datadir} -walletdir=#{bitcoin_walletdir} -pid=#{bitcoin_datadir}/bitcoind.pid",
 | 
						|
      PIDFile: "#{bitcoin_datadir}/bitcoind.pid",
 | 
						|
      Restart: 'always',
 | 
						|
      PrivateTmp: true,
 | 
						|
      LimitNOFILE: 'infinity',
 | 
						|
      TimeoutStopSec: '60s',
 | 
						|
      TimeoutStartSec: '20s',
 | 
						|
      StartLimitInterval: '60s',
 | 
						|
      StartLimitBurst: '2'
 | 
						|
    },
 | 
						|
    Install: {
 | 
						|
      WantedBy: 'multi-user.target'
 | 
						|
    }
 | 
						|
  })
 | 
						|
  verify false
 | 
						|
  triggers_reload true
 | 
						|
  action [:create, :enable, :start]
 | 
						|
end
 | 
						|
 | 
						|
# TODO move to custom kosmos cookbook before publishing bitcoin cookbook
 | 
						|
firewall_rule 'bitcoind' do
 | 
						|
  port     [8333] # TODO adjust for testnet
 | 
						|
  protocol :tcp
 | 
						|
  command  :allow
 | 
						|
end
 |