macOS Sierraにawscliインストールしてみる
pipのインストール
pip installationで、get-pip.pyをダウンロードします。
sudo python get-pip.py
AWS CLIをインストール
sudo pip install awscli --upgrade --ignore-installed six
AWS Access Key IDとAWS Secret Access Keyの取得
IAMでユーザを作成して、AWS Access Key IDとAWS Secret Access Keyの取得
リージョンを調べる
awsコマンドの初期設定
aws configure AWS Access Key ID [None]: xxx AWS Secret Access Key [None]: xxx Default region name [None]: ap-northeast-1 Default output format [None]:
設定した内容の確認
cat .aws/config cat .aws/credentials
VPCでsubnet-idを調べる
EC2ダッシュボードで、キーペアを作成(MyKeyPair)
インスタンスの作成
とりあえず、デフォルトVPCで作成
# RDS aws rds create-db-instance \ --db-instance-identifier testdb \ --db-instance-class db.t2.micro \ --engine mysql \ --engine-version 5.7.11 \ --master-username dbuser \ --master-user-password password \ --allocated-storage 5 # AP aws ec2 run-instances \ --image-id ami-831fcde2 \ --count 1 \ --instance-type t2.micro \ --key-name MyKeyPair \ --security-group-ids sg-xxx \ --subnet-id subnet-xxx aws ec2 run-instances \ --image-id ami-831fcde2 \ --count 1 \ --instance-type t2.micro \ --key-name MyKeyPair \ --security-group-ids sg-xxx \ --subnet-id subnet-xxx
TypeScriptはじめました
GitHubリポジトリ
動かし方
npm install npm start
動くようになるまでにしたこと
初期化
npm init typings init
最初にcrypto-jsとその型定義
npm install crypto-js --save typings install dt~cryptojs --save --global
tsconfig.jsonを記述
{ "compilerOptions": { "target": "es5", "module": "commonjs", "moduleResolution": "node", "removeComments": false }, "files": [ "typings/index.d.ts" ] }
app/main.tsを書く前に
これだけだと、requireファンクションを使うことができなかった。 Angular2のtypings.jsonをみて、少し追加。
typings install dt~node --save --global
これで、requireが使えるようになる。 だけど、とりあえず、Angular2を参考にcore-jsとjasmineも追加しておくことにした。
typings install dt~core-js --save --global typings install dt~jasmine --save --global
app/main.tsを書く
Visual Studio Codeを使ってさくさくっと記述。型があるのは補完とかがきいて便利。
Seleniumメモ
Azure IoT HubのMQTT(s)
環境
MQTTに接続するユーザ名とパスワード
ユーザ名
{iothubhostname}/{device_id}
パスワード
SharedAccessSignature sig={signature-string}&se={expiry}&sr={URL-encoded-resourceURI}
String password = String.format( "SharedAccessSignature sig=%s&se=%s&sr=%s", URLEncoder.encode(sig, StandardCharsets.UTF_8.name()), expiry, scope);
expiry
有効期限でUNIXタイムスタンプ(秒)のlong値です
URL-encoded-resourceURI(scope)
{iothubhostname}/devices/{device_id}
signature-string(sig)
デバイスのキーをBase64でデコードしたバイナリをHMAC-SHA256のキーとして使用します。
String deviceKey = properties.getDeviceKey(); Mac mac = Mac.getInstance("HmacSHA256"); mac.init(new SecretKeySpec(Base64.getMimeDecoder().decode(deviceKey), "HmacSHA256"));
URL-encoded-resourceURIとexpiryを改行で分けたメッセージを署名します
String scope = String.format("%s/devices/%s", properties.getHostname(), URLEncoder.encode(properties.getDeviceId(), StandardCharsets.UTF_8.name())); String message = String.format("%s\n%s", scope, expiry); String sig = Base64.getMimeEncoder().encodeToString(mac.doFinal(message.getBytes()));
IoT Hubへのメッセージの送信
devices/{device_id}/messages/events/ または devices/{device_id}/messages/events/{property_bag} をトピック名としてメッセージを送信できます。
IoT Hubからのメッセージの受信
devices/{device_id}/messages/devicebound/# をトピックフィルターとして使用してサブスクライブできます。
GitHub
Jackson
Jacksonを使って、Java Objectをjsonに変換する場合のコードです。Java Objectはpublicのフィールドがあればgetterはなくてもよいです。
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(bean);
jsonには、beanのプロパティ名をキーとするイメージが出力されます。プロパティ名と異なるキーにする場合には、@JsonPropertyを使用することができます。
public class Bean { @JsonProperty("user_id") public Integer id; public String name; }
このBeanクラスからjsonに変換したイメージは下のようになります。
{"user_id":null,"name":null}
特定のクラスや特定のフィールドのnull値の項目のキーが生成されないようにしたい場合は、クラスまたはフィールドにアノテーションを記述します。
@JsonInclude(JsonInclude.Include.NON_NULL) public class Bean {
@JsonInclude(JsonInclude.Include.NON_NULL) public String name;
または、ObjectMapperを設定してnull値の項目のキーが生成されないようにできます。
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
String json = mapper.writeValueAsString(bean);
Spring Bootアプリケーション
ObjectMapperにInclude.NON_NULLを設定したい場合は、application.propertiesに記述します。
spring.jackson.serializationInclusion=NON_NULL
Javaアプリケーションのリモートデバッグ
Spring Bootアプリケーションをリモートデバッグしたときのメモ
$ java -agentlib:jdwp=transport=dt_socket,server=y,address=8000 -jar application.jar
Gradleでのプロパティファイルの拡張(expand)
Spring Bootアプリケーションで、検証環境と本番環境とでプロパティの値を変更するためのビルドスクリプトを書いてみました。
GradleのドキュメントとSpring Bootのリファレンスを参考にしています。
ここでは、例としてspring.profiles.activeの値を設定してみます。application.propertiesには次のように書きます。
spring.profiles.active=${ext.profiles}
次に、build.gradleにprocessResourcesを記述します。
processResources { filesMatching('**/application.properties') { expand(project.properties) } }
-Pprofiles=fooを指定してbuildを実行します。 build/resourcesにコピーされたapplication.propertiesは下のようになります。
spring.profiles.active=foo
置換してほしくないプロパティについては、$の前に(バックスラッシュ)を付加します。
spring.datasource.url=\${url}
build/resourcesにコピーされたプロパティファイルは下のようになります。
spring.datasource.url=${url}