How to interact with the Chef Server using the Chef Server API using Shell script
!/usr/bin/env bash
_chef_dir ()
{
# Helper function:
# Recursive function that searches for chef configuration directory
# It looks upward from the cwd until it hits /. If no directory is found,
# ~/.chef is chosen if it exists
# You could simply hard-code the path below
if
[
"$PWD"
=
"/"
]; then
if
[ -d ".chef"
]; then
echo
"/.chef"
elif
[ -d "$HOME/.chef"
]; then
echo
"$HOME/.chef"
fi
return
fi
if
[ -d '.chef'
];then
echo
"${PWD}/.chef"
else
(cd ..; _chef_dir)
fi
}
_chomp ()
{
# helper function to remove newlines
awk '{printf "%s", $0}'
}
chef_api_request()
{
# This is the meat-and-potatoes, or rice-and-vegetables, your preference really.
local method path body timestamp chef_server_url client_name hashed_body hashed_path
local canonical_request headers auth_headers
chef_server_url="https://api.opscode.com/organizations/my_org"
# '/organizations/ORG_NAME' is needed for Hosted Chef or Private Chef
if echo
$chef_server_url | grep -q "/organizations/" ; then
endpoint=/organizations/${chef_server_url#*/organizations/}${2%%\?*}
else
endpoint=${$2%%\?*}
fi
path=${chef_server_url}$2
client_name="chef_user"
method=$1
body=$3
hashed_path=$(echo -n "$endpoint" | openssl dgst -sha1 -binary | openssl enc -base64)
hashed_body=$(echo -n "$body" | openssl dgst -sha1 -binary | openssl enc -base64)
timestamp=$(date -u "+%Y-%m-%dT%H:%M:%SZ")
canonical_request="Method:$method\nHashed Path:$hashed_path\nX-Ops-Content-Hash:$hashed_body\nX-Ops-Timestamp:$timestamp\nX-Ops-UserId:$client_name"
headers="-H X-Ops-Timestamp:$timestamp \
-H X-Ops-Userid:$client_name \
-H X-Chef-Version:0.10.4 \
-H Accept:application/json \
-H X-Ops-Content-Hash:$hashed_body \
-H X-Ops-Sign:version=1.0"
auth_headers=$(printf
"$canonical_request" | openssl rsautl -sign -inkey \
"$(_chef_dir)/${client_name}.pem" | openssl enc -base64 | _chomp | awk '{ll=int(length/60);i=0; \
while (i<=ll) {printf " -H X-Ops-Authorization-%s:%s", i+1, substr($0,i*60+1,60);i=i+1}}')
case
$method in
GET)
curl_command="curl $headers $auth_headers $path"
$curl_command
;;
*)
echo
"Unknown Method. I only know: GET" >&2
return 1
;;
esac
}
chef_api_request "$@"
How to interact with the Chef Server using the Chef Server API using Shell script的更多相关文章
- Server Tomcat v7.0 Server at localhost was unable to start within 45 seconds
错误:Server Tomcat v7.0 Server at localhost was unable to start within 45 seconds 错误提示就是我们限定了部署的时间导致的错 ...
- Step by step Install a Local Report Server and Remote Report Server Database
原创地址:http://www.cnblogs.com/jfzhu/p/4012097.html 转载请注明出处 前面的文章<Step by step SQL Server 2012的安装 &g ...
- Server Tomcat v7.0 Server at localhost was unable to&nbs 报错问题解决
在eclipse启动tomcat时遇到超时45秒的问题: Server Tomcat v7.0 Server at localhost was unable to start within 45 se ...
- Comparison of SQL Server Compact, SQLite, SQL Server Express and LocalDB
Information about LocalDB comes from here and SQL Server 2014 Books Online. LocalDB is the full SQL ...
- Server Tomcat v6.0 Server at localhost was unable to start within 45 seconds...
仰天长啸 Server Tomcat v6.0 Server at localhost was unable to start within 45 seconds... 当启动tomcat时候出现 ...
- Server Tomcat v7.0 Server at localhost was unable to start within 45 seconds -----》myeclipse2015
错误:Server Tomcat v7.0 Server at localhost was unable to start within 45 seconds 错误提示就是我们限定了部署的时间导致的错 ...
- Server Tomcat v6.0 Server at localhost was unable to start within 45 seconds问题
错误:Server Tomcat v6.0 Server at localhost was unable to start within 45 seconds. If the server requi ...
- Server Tomcat v7.0 Server at localhost was unable to&nbs 报错问题解决
在eclipse启动tomcat时遇到超时45秒的问题: Server Tomcat v7.0 Server at localhost was unable to start within 45 se ...
- Server Tomcat v7.0 Server at localhost failed to start.临时解决办法
错误名:Server Tomcat v7.0 Server at localhost failed to start. 解决办法:去掉下面这句话: (通常在代码开头部分,public class前) ...
随机推荐
- Java虚拟机的内存组成以及堆内存介绍
一.java内存组成介绍:堆(Heap)和非堆(Non-heap)内存 按照官方的说法:“Java 虚拟机具有一个堆,堆是运行时数据区域,所有类实例和数组的内存均从此处分配.堆是在 Java 虚拟机启 ...
- 初学者的checklist:对于QTP,你应该知道的9个基本概念
学习QTP或者其他相关任何工具的方法都是首先把基本的概念过一遍.正所谓砍柴不怕磨刀功,一旦你对这些概念熟悉了,你就可以学习该工具的高级部分了.写这篇文章的目标是列出初学QTP的人应该掌握的所有基本概念 ...
- smartcomb:用php实现的web模块拼合器
smartcomb是一个用php实现的web模块拼合器,相对于其他的代码拼合工具,如下特性: 可以拼合任意类型的文件,不限于js文件. 集中并声明依赖,自动分析依赖拼合,按需加载. 支持多种配置切换 ...
- maven小项目注册服务(三)--web模块
java的web应用打包方式一般为war它和jar的区别就是包含了更多的资源和文件,如JSP文件,静态资源文件,servlet等等.war包的核心就WEB-INF文件夹下必须有一个web.xml 的配 ...
- java--面向抽象编程
所谓面向抽象编程是指当设计某种重要的类时,不让该类面向具体的类,而是面向抽象类,及所设计类中的重要数据是抽象类声明的对象,而不是具体类声明的对象.就是利用abstract来设计实现用户需求. 比如:我 ...
- OAuth2.0和SSO授权的区别
OAuth2.0和SSO授权 一.OAuth2.0授权协议 一种安全的登陆协议,用户提交的账户密码不提交到本APP,而是提交到授权服务器,待服务器确认后,返回本APP一个访问令牌,本APP即可用该 ...
- linux/unix网络编程之epoll
转载自 Linux epoll模型 ,这篇文章讲的非常详细! 定义: epoll是Linux内核为处理大批句柄而作改进的poll,是Linux下多路复用IO接口select/poll的增强版本,它能显 ...
- 1124. Mosaic(dfs)
1124 需要想那么一点点吧 一个连通块中肯定不需要伸进手不拿的情况 不是一个肯定会需要这种情况 然后注意一点 sum=0的时候 就输出0就可以了 不要再减一了 #include <iostre ...
- BZOJ3105: [cqoi2013]新Nim游戏
题解: 线性基?类似于向量上的基底. 此题题解戳这里:http://blog.csdn.net/wyfcyx_forever/article/details/39477673 代码: #include ...
- 域名下Web项目重定向出现DNS域名解析错误问题
问题: 项目使用的是阿里云的ESC,前几天为IP地址添加了域名 发现发送正常请求时跳转没问题,但发送重定向请求时,页面就会出现DNS域名解析错误的情况 1.我在Tomcat的server.xml中配置 ...