!/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的更多相关文章

  1. 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 错误提示就是我们限定了部署的时间导致的错 ...

  2. 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 ...

  3. Server Tomcat v7.0 Server at localhost was unable to&amp;nbs 报错问题解决

    在eclipse启动tomcat时遇到超时45秒的问题: Server Tomcat v7.0 Server at localhost was unable to start within 45 se ...

  4. 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 ...

  5. 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时候出现 ...

  6. 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 错误提示就是我们限定了部署的时间导致的错 ...

  7. 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 ...

  8. 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 ...

  9. Server Tomcat v7.0 Server at localhost failed to start.临时解决办法

    错误名:Server Tomcat v7.0 Server at localhost failed to start. 解决办法:去掉下面这句话: (通常在代码开头部分,public class前) ...

随机推荐

  1. NSDictionary 初始化

    NSDictionary *dic1=[NSDictionary dictionaryWithObject:@"1" forKey:@"a"];         ...

  2. linux协议栈部分资料

    socket实现:http://blog.csdn.net/zhangskd(含有tcp/ip专栏) http://blog.chinaunix.net/uid-20528014-id-90573.h ...

  3. Hibernate学习笔记(1)

    1 使用Hibernate (1)创建User Library,命名为HIBERNATE3,加入需要的jar (2)创建hibernate配置文件hibernate.cfg.xml, 为了便于调试最好 ...

  4. 对于接收到的GPS信息详解

    最近一直在做gps驱动方面的东西,对于底层接收到的gps信息不是很了解,查询了资料对这些信息做出总结: 由于在室内,所以信号不是很好,接收不到卫星信号,必须站到窗口或者空旷的地方,这是gps的debu ...

  5. ehcache版本冲突

    以ehchache-core2.5为分水岭 缓存版本问题 版本不一样 配置不一样  ehcache-core-2.4.3.jar 与 ehcache-core-2.6.6 一 Caused by: n ...

  6. jQuery EasyUI - Add link to datagrid cell

    Extracted from: http://stackoverflow.com/questions/16061894/jquery-easyui-add-link-to-cell HTML: < ...

  7. 如何在Ubuntu上安装最新版本的Node.js

    apt-get update apt-get install -y python-software-properties software-properties-common add-apt-repo ...

  8. fzu Problem 2140 Forever 0.5(推理构造)

    题目:http://acm.fzu.edu.cn/problem.php?pid=2140 题意: 题目大意:给出n,要求找出n个点,满足: 1)任意两点间的距离不超过1: 2)每个点与(0,0)点的 ...

  9. ASP.NET MVC 学习3、Controller左手从Model获取数据,右手传递到View页面

    参考:http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/accessing-your-models-dat ...

  10. Test语言编译器V0.8

    感觉这个挺好耍的,书上的代码有错误,而且功能有限. 一.词法分析 特点: (1)可对中文进行识别:(2)暂不支持负数,可以在读入‘-'时进行简单标记后就能对简单负数进行识别了. #include &l ...