]> git.openstreetmap.org Git - chef.git/blob - cookbooks/oxidized/recipes/default.rb
oxidized: fix task ordering
[chef.git] / cookbooks / oxidized / recipes / default.rb
1 #
2 # Cookbook:: oxidized
3 # Recipe:: default
4 #
5 # Copyright:: 2022, OpenStreetMap Foundation
6 #
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
10 #
11 #     https://www.apache.org/licenses/LICENSE-2.0
12 #
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18 #
19
20 include_recipe "git"
21 include_recipe "ruby"
22
23 package %w[
24   gcc
25   g++
26   make
27   cmake
28   libssl-dev
29   libssh2-1-dev
30   zlib1g-dev
31   pkg-config
32 ]
33
34 keys = data_bag_item("oxidized", "keys")
35 devices = data_bag_item("oxidized", "devices")
36
37 directory "/etc/oxidized" do
38   owner "root"
39   group "root"
40   mode "755"
41 end
42
43 template "/etc/oxidized/config" do
44   source "config.erb"
45   owner "oxidized"
46   group "oxidized"
47   mode "444"
48   notifies :restart, "service[oxidized]"
49 end
50
51 template "/etc/oxidized/routers.db" do
52   source "routers.db.erb"
53   owner "oxidized"
54   group "oxidized"
55   mode "400"
56   variables :devices => devices
57   notifies :restart, "service[oxidized]"
58 end
59
60 directory "/var/log/oxidized" do
61   owner "oxidized"
62   group "oxidized"
63   mode "755"
64 end
65
66 git "/opt/oxidized" do
67   action :sync
68   repository "https://github.com/openstreetmap/oxidized.git"
69   depth 1
70   user "oxidized"
71   group "oxidized"
72   notifies :run, "bundle_install[/opt/oxidized]"
73 end
74
75 # Key is set as a deployment key in github repo
76 file "/opt/oxidized/.ssh/id_rsa" do
77   content keys["git"].join("\n")
78   owner "oxidized"
79   group "oxidized"
80   mode "400"
81   notifies :delete, "file[/opt/oxidized/.ssh/id_rsa.pub]", :immediately
82   notifies :restart, "service[oxidized]"
83 end
84
85 # Ensure public key is deleted if private key is changed. Trigged by notify
86 file "/opt/oxidized/.ssh/id_rsa.pub" do
87   action :nothing
88 end
89
90 execute "/opt/oxidized/.ssh/id_rsa.pub" do
91   command "ssh-keygen -f /opt/oxidized/.ssh/id_rsa -y > /opt/oxidized/.ssh/id_rsa.pub"
92   user "oxidized"
93   group "oxidized"
94   creates "/opt/oxidized/.ssh/id_rsa.pub"
95   notifies :restart, "service[oxidized]"
96 end
97
98 git "/var/lib/oxidized/configs.git" do
99   action :sync
100   repository "git@github.com:openstreetmap/oxidized-configs.git" # Uses oxidized ssh key
101   checkout_branch "master" # branch is hardcoded in oxidized
102   user "oxidized"
103   group "oxidized"
104 end
105
106 bundle_install "/opt/oxidized" do
107   action :nothing
108   options "--deployment"
109   user "oxidized"
110   group "oxidized"
111   notifies :restart, "service[oxidized]"
112 end
113
114 # Based on https://github.com/ytti/oxidized/blob/master/extra/oxidized.service
115 systemd_service "oxidized" do
116   description "oxidized network device backup daemon"
117   after "network.target"
118   user "oxidized"
119   working_directory "/opt/oxidized"
120   exec_start "#{node[:ruby][:bundle]} exec oxidized"
121   environment "OXIDIZED_HOME" => "/etc/oxidized",
122               "OXIDIZED_LOGS" => "/var/log/oxidized"
123   nice 10
124   private_tmp true
125   private_devices true
126   protect_system "full"
127   protect_home true
128   no_new_privileges true
129   restart "on-failure"
130   notifies :restart, "service[oxidized]"
131 end
132
133 service "oxidized" do
134   action [:enable, :start]
135 end
136
137 template "/etc/logrotate.d/oxidized" do
138   source "logrotate.erb"
139   owner "root"
140   group "root"
141   mode "644"
142 end