구리의 창고

Chef resource 실행 순서 with notifies 본문

DevOps

Chef resource 실행 순서 with notifies

구리z 2017. 7. 6. 14:43

소개

Chef Recipe를 작성하거나, 오픈소스를 보다보면 notifies property를 아래와 같이 사용한 부분을 많이 볼 수 있다.
template '/etc/nginx/nginx.conf' do
  notifies :restart, 'service[nginx]', :delayed
end

notifies는 template resource가 실행 될 때, 다른 resource에 어떤 영향을 줄 지에 대한 정책을 정해준다.

하지만, 실행되는 순서가 약간 헷갈려서 정리하려고 한다.

notifies

기본적으로 notifies는 아래와 같은 형태를 갖는다.
notifies action, resource, timer
action: resource에 따라 다르다.
resource: resource대상을 적어준다.
timer: :delayed, :before, :immediate, :immediately (생략 할 경우, 기본 값 :delayed)

timer에 대한 간단한 예제는 아래와 같다.

service[nginx] resource를 바로 실행한다.
notifies :restart, 'service[nginx]', :immediately
service[nginx] resource를 chef-client 마지막에 실행한다.
notifies :stop, 'service[nginx]', :delayed
service[nginx] resource를 바로 전에 실행한다.
notifies :stop, 'service[nginx]', :before
아래에서는 여러 notifies에 따른 순서를 예를 들어 설명하겠다.

예제 코드1

이 코드에서 resource는 2개가 있다. tempate['/etc/nginx/nginx.conf'] service['nginx']
template '/etc/nginx/nginx.conf' do
  notifies :restart, 'service[nginx]', :delayed
end

service 'nginx' do
  action :nothing
end
여기서 service['nginx'] action이 nothing 이므로 notifies에 의해서만 실행이된다.
Recipe가 실행되면 resource의 순서는 아래와 같다.
1. tempate['/etc/nginx/nginx.conf'] (코드 순서에 따른 실행)
2. service['nginx'] :restart (코드 순서에 따른 실행)

예제 코드2

예제 코드1에서 service['nginx'] resource의 action을 stop으로 바꿔보겠다.
template '/etc/nginx/nginx.conf' do
  notifies :restart, 'service[nginx]', :delayed
end

service 'nginx' do
  action :stop
end

:delayed의 경우 chef-client 마지막에 실행된다고 했으므로 resource 순서는 아래와 같다.
1. template['/etc/nginx/nginx.conf'] (코드 순서에 따른 실행)
2. service['nginx'] :stop (코드 순서에 따른 실행)
3. service['nginx'] :restart (notifies에 따른 chef-client 마지막에 실행)

예제 코드3

이번 코드에는 resource를 하나더 추가하고 service['nginx'] resource의 action을 :nothing으로 변경했다.

package 'nginx'

template '/etc/nginx/nginx.conf' do
  notifies :restart, 'service[nginx]', :delayed
end

service 'nginx' do
  action :nothing
end

1. package['nginx'] (코드 순서에 따른 실행)
2. template['/etc/nginx/nginx.conf'] (코드 순서에 따른 실행)
3. service['nginx'] :restart (notifies에 따른 chef-client 마지막에 실행)

예제 코드4

이번 코드에는 notifies를 하나 더 추가하겠다. notifies 정책이 여러 개면 큐에 저장했다.가 차례 대로 실행된다.
package 'nginx'

template '/etc/nginx/nginx.conf' do
  notifies :stop, 'service[nginx]', :delayed
  notifies :start, 'service[nginx]', :delayed
end

service 'nginx' do
  action :nothing
end
1. package['nginx'] (코드 순서에 따른 실행)
2. template['/etc/nginx/nginx.conf'] (코드 순서에 따른 실행)
3. service['nginx'] :stop(notifies에 따른 chef-client 마지막에 실행)
4. service['nginx'] :start(notifies에 따른 chef-client 마지막에 실행)

정리

코드 실행 순서는 아래와 같다

1. 일단 기본적으로 Recipe는 위에서 아래 순서로 실행된다.
2. notifies 정책이 before면 해당 resource 전에 실행된다.
3. notifies 정책이 immediate면 해당 resource 다음에 실행된다.
4. notifies 정책이 delayed면 chef-client 마지막에 실행된다.
5. notifies가 여러 개면 차례대로 큐에 저장해놓고 실행된다.

'DevOps' 카테고리의 다른 글

docker-compose 사용  (0) 2017.07.12
docker-compose 설치  (0) 2017.07.12
Docker Container 실행하기  (0) 2017.07.08
Ubuntu에 Docker 설치하기 with Chef  (0) 2017.07.05
Ubuntu에 Docker 설치하기  (0) 2017.07.03
Comments